2012-07-27 61 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

相關問題