2012-07-27 59 views
1

我正在尝试将Uploadifive实现为Django。这对我来说很困难,因为我对Django不太了解。一点儿都没有。这当然应该告诉我,我应该等这个,并首先学习更多的Django,但..是的,我将无法远离它..任何使用Django的Uploadifive的经验?

据我所知,我需要看看Uploadifive软件包附带的PHP脚本,并在Django视图中编写相应的代码。这是我的猜测,至少..问题是,我似乎无法找到任何在线指南,或任何提示如何做到这一点。有没有人有这样的例子,我可以看看?或者我应该去哪里的任何提示?

到目前为止,我已经在urls.py中创建了一个模式,它将浏览器定向到一个将用户发送到正确网页的views.py def。 JavaScript在网站上初始化(我可以看到“选择文件”按钮),但是当我选择图像时,什么都不会发生。我试图根据我找到的基于闪存的Uploadify指南构建脚本,但这不起作用。

编辑:有人指出,自然有必要看看PHP代码实际上做了什么。这确实是一个付费软件,但我无法想象这里的PHP代码是代码的重要组成部分,它会“放弃”任何东西。无论如何,真正的工作在于JavaScript文件。

上传PHP脚本收件人:

<?php 
/* 
UploadiFive 
Copyright (c) 2012 Reactive Apps, Ronnie Garcia 
*/ 

// Set the uplaod directory 
$uploadDir = '/uploads/'; 

// Set the allowed file extensions 
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions 

if (!empty($_FILES)) { 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir; 
    $targetFile = $uploadDir . $_FILES['Filedata']['name']; 

    // Validate the filetype 
    $fileParts = pathinfo($_FILES['Filedata']['name']); 
    if (in_array(strtolower($fileParts['extension']), $fileTypes)) { 

     // Save the file 
     move_uploaded_file($tempFile,$targetFile); 
     echo 1; 
    } else { 
     // The file type wasn't allowed 
     echo 'Invalid file type.'; 
    } 
} 
?> 

模板:

{% extends 'base/index.html' %} 
    {% block stylesheet %} 
     <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}uploadifive/uploadifive.css"> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> 
     <script type="text/javascript" src="{{ STATIC_URL }}uploadifive/jquery.uploadifive.js"></script> 
     <script type="text/javascript"> 
     $(document).ready(function(){ 
     $('#file_upload').uploadifive({ 
      'debug': true, 
      'formData':{'test':'something'}, 
      'uploadScript': '/upload/', 
      'queueID': 'queue', 
      'cancelImage': '{{ STATIC_URL }}uploadifive/uploadifive-cancel.png', 
      'auto': true 
     }); 
     }); 
     </script> 
    {% endblock %} 

    {% block content %} 
     <form> 
     <input type="file" name="file_upload" id="file_upload" /> 
     </form> 
    {% endblock %} 

views.py

from django.shortcuts import render 
    from gallery.models import Pic,Comment,Tag 
    from django.conf import settings 
    from django.http import HttpResponse 
    from django.shortcuts import render 

    def upload(request): 
     if request.method == 'POST': 
      for field_name in request.FILES: 
       uploaded_file = request.FILES[field_name] 

       #write the file into destination 
       destination_path = '<absolute path to project>/media/gallery/pictures/%s' % (uploaded_file.name) 
       destination = open(destination_path, 'wb+') 
       for chunk in uploaded_file.chunks(): 
        destination.write(chunk) 
       destination.close() 

      #indicate that everything is OK 
      return HttpResponse("ok", mimetype="text/plain") 
     else: 
      #show the upload UI 
      return render(request, 'gallery/upload.html') 
+0

问题是,这是一个付费软件包,因此如果我们看不到实际的PHP代码,就很难告诉您如何正确地将PHP转换为Python。对不起,我不支付5美元来帮助你;)。你可以检查你的许可证,看看它是否可能允许你在这里发布脚本,或者甚至可能足以让你开始。否则,我会向开发者抱怨他们应该添加Python支持。 – 2012-07-27 14:31:03

+0

@ChrisPratt - 你当然是对的。我编辑了这个问题,现在输入PHP位。就像我在这个问题中写的那样; PHP位非常小,基本真实。购买的软件真正的魔力在于JavaScript。 – 2012-07-27 18:47:06

回答

1

这里的PHP文件的我的Python翻译:

# Set the uplaod directory 
upload_dir = '/absolute/path/to/upload/dir/' 

# Set the allowed file extensions 
file_types = ['jpg', 'jpeg', 'gif', 'png'] # Allowed file extensions 

if len(request.FILES): 
    target_file = upload_dir + request.FILES['field_name'].name 

    # Validate the filetype 
    filename, ext = os.path.splitext(request.FILES['field_name'].name) 
    if ext.lower() in file_types: 

     # Save the file 
     with open(target_file, 'wb+') as destination: 
      for chunk in request.FILES['field_name'].chunks(): 
       destination.write(chunk) 
    else: 
     # The file type wasn't allowed 
     print 'Invalid file type.' 

如果有一次发送多个文件,它可能会更好,只是包装的代码在for循环:

for file in request.FILES.values(): 
    target_file = upload_dir + file.name 
    ... 

然后,你显然希望返回正确的响应对象和处理错误的更好,但你应该能够自己处理。

+0

非常感谢你:)我试着输入并运行它。它似乎没有提出任何错误,但它似乎也没有做任何事情。我尝试在这里和那里输入一些照片,看看是否有任何东西出来,但它没有。就好像这个视图根本没有启动。除非我正在寻找完全错误的地方,这是不可能的完全错误的地方.. – 2012-07-27 19:58:56

相关问题