在你继续阅读,相信我,当我说我看过所有关于这一问题的其他职位,其中没有帮助。
Before you continue reading, trust me when I say I have read all the other posts on this subject, and none of them helped.
我想图片上传功能添加到我的网站。我要上传的图片 通过Ajax职位。我不能得到这个工作。
I am trying to add image upload functionality to my website. I want to upload the image via an ajax post. I cannot get this working.
下面是我:
HTML - 我有一个特殊的设置,使图像显示一个愚蠢的按钮,而不是 且文本字段。我也使用onChange事件选择图像后,当我击中确定即可自动提交。
HTML - i have a special setup so that an image is displayed instead of a stupid button and the text field. I am also using the onChange event to automatically submit when I have hit "OK" after selecting the image.
<form id="add-picture-form" method="POST" action="/api/upload_image/" enctype="multipart/form-data">{% csrf_token %}
<div class="thumbnails" style="width:400px;">
<label class="cabinet BrandHeader">
<input type="file" class="file" id="upload-photo" onChange="$('#add-picture-form').submit();" />
</label>
</div>
</form>
jQuery的:
Jquery:
$('#add-picture-form').submit(function() {
//var filename = $("#upload-photo").val();
var photo = document.getElementById("upload-photo");
var file = photo.files[0];
$.ajax({
type: "POST",
url: "/api/upload_image/",
enctype: 'multipart/form-data',
data: {'file': file.getAsBinary(), 'fname' : file.fileName },
success: function(){
alert( "Data Uploaded: ");
}
});
return false;
});
最后我Django的看法,当你发布到被打/ API / upload_image /
Finally my django view that is hit when you post to /api/upload_image/
def ajax_upload( request ):
print request.POST
print request.FILES
return http.HttpResponse(simplejson.dumps([True]), mimetype='application/javascript')
我试图写的图像二值,但我无法打开已写入的数据。 使用JavaScript这么辛苦为什么上传图片?我是一个白痴,只是没有使用一个简单的解决方案?如果有,请告诉我什么是使用jQuery上传图片在Django的最佳方式。
I have tried to write the image to binary, but I cannot open that data that has written. Why is uploading an image using javascript so hard? I am an idiot and just not using a simple solution? If so, please tell me what is the best way to use jQuery to upload an image in Django.
尝试jQuery插件 Uploadify 或的 SWFUpload的。甚至有人为你做的Django的集成,请参阅: https://github.com/tstone/django-uploadify 和http://blog.fogtunes.com/2009/11/howto-integrate-swfupload-with-django/.
Try the jQuery plugins Uploadify or SWFUpload. Someone even did the Django integration for you, see: https://github.com/tstone/django-uploadify and http://blog.fogtunes.com/2009/11/howto-integrate-swfupload-with-django/.
上一篇:Django的CSRF令牌没有形式令牌、形式、Django、CSRF
下一篇:ID VS的UniqueID VS客户端ID VS UniqueClientID VS StaticClientID?客户端、VS、ID、UniqueID