我正试图在Django实施Uploadifive。这对我来说很困难,因为我和Django的关系不是很好。一点都没有。当然,这应该告诉我,我应该用这个等着,先学到更多的Django,但是..。是的,我无法远离它..。
据我所知,我需要看看Uploadifive包附带的PHP脚本,并在Django视图中编写相应的脚本。这是我的猜测至少..。问题是,我似乎在网上找不到任何指南,也找不到任何关于如何做的提示。有谁有这样的例子,我可以看看吗?或者我该去哪有什么建议吗?
到目前为止,我已经在urls.py中创建了一种模式,该模式将浏览器定向到将用户发送到正确网页的views.py def。JavaScript在站点上初始化(我可以看到“选择文件”按钮),但是当我选择图像时,什么都不会发生。我试图根据我找到的基于flash的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')发布于 2012-07-27 19:04:05
下面是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
...然后,您显然希望返回正确的响应对象并更好地处理错误,但是您应该能够自己处理。
https://stackoverflow.com/questions/11689679
复制相似问题