我正在嘗試將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')
問題是,這是一個付費軟件包,因此如果我們看不到實際的PHP代碼,就很難告訴您如何正確地將PHP轉換爲Python。對不起,我不支付5美元來幫助你;)。你可以檢查你的許可證,看看它是否可能允許你在這裏發佈腳本,或者甚至可能足以讓你開始。否則,我會向開發者抱怨他們應該添加Python支持。 – 2012-07-27 14:31:03
@ChrisPratt - 你當然是對的。我編輯了這個問題,現在輸入PHP位。就像我在這個問題中寫的那樣; PHP位非常小,基本真實。購買的軟件真正的魔力在於JavaScript。 – 2012-07-27 18:47:06