django ajax上传文件
【摘要】
文件上传
https://www.bbsmax.com/A/l1dyQexdem/
<script type="text/javascript">
function uploadImage() {
...
文件上传
<script type="text/javascript">
function uploadImage() {
$.ajaxFileUpload({
url: "/Struts2Upload/test/uploadAjax.action",
secureuri:false,
fileElementId:"btn_file",
data:{lbg:"json传值并且返回"},
dataType: "json",
error: function(data, status, e) {
alert("shibai");
alert(e);
},
success: function(data, textStatus) {
alert(textStatus);
$('img').attr('src',data.imPath);
}
});
}
</script>
<input type="file" id="btn_file" name="btn_file" οnchange="uploadImage();">
处理图片:
def pic_class(request): upload_file = request.FILES.get("myfile", None) # 获取上传的文件,如果没有文件,则默认为None file_obj = request.FILES.get('file') if file_obj: # 处理附件上传到方法 request_set = {} print('file--obj', file_obj) # user_home_dir = "upload/%s" % (request.user.userprofile.id) # accessory_dir = settings.accessory_dir # if not os.path.isdir(accessory_dir): # os.mkdir(accessory_dir) scr = Image.open(file_obj) img= np.asarray(scr)
with open(upload_file, 'wb') as new_file: for chunk in file_obj.chunks(): new_file.write(chunk)
### get the inmemory file
data = request.FILES.get('btn_file') # get the file from the curl
### write the data to a temp file
tup = tempfile.mkstemp() # make a tmp file
f = os.fdopen(tup[0], 'w') # open the tmp file for writing
f.write(data.read()) # write the tmp file
f.close()
### return the path of the file
filepath = tup[1] # get the filepath
return filepath
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/77996205
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)