form
templates
<form enctype=\"multipart/form-data\" method=\"POST\" action=\"******\">
<input type=\"file\" name=\"file\" />
<br />
<input type=\"submit\" value=\"上传文件\" />
</form>
views
def upload(request):
file = request.FILES.get(\'file\')
path=os.path.join(\'static/fileup\',file.name)
f=open(path,\'wb\')
for chunk in file.chunks(chunk_size=1024):
f.write(chunk)
f.close()
return render(request, \'upload_Succee.html\')
ajax
templates
<input type=\"file\" name=\"file\" />
<a =\"uploadFile2();\" style=\"cursor: pointer; display: inline-block;>JQuery-Ajax上传</a>
< >
function uploadFile2() {
#Jquery转换为dom对象:$(\"#img\")[0].files[0];其中$(\"#img\")是jquery对象,$(\"#img\")[0]是dom对象
var fileobj = $(\"type=\'file\'\")[0].files[0];
var form = new FormData();
form.append(\"file\", fileobj);
form.append(\"uesr\", \'jw\');
$.ajax({
type: \'POST\',
url: \'******\',
data: form,
processData: false, # 告诉jquery要传输data对象
contentType: false, # 告诉jquery不需要增加请求头对于contentType的设置
}).done(fuction(data){
#data为返回的json对象
#请求成功执行的代码块
}).fail(function(){
#请求失败执行的代码块
})
}
</ >
views
from django.http import JsonResponse
import os
def upload(request):
file = request.FILES.get(\'file\')
path=os.path.join(\'static/fileup\',file.name)
f=open(path,\'wb\')
for chunk in file.chunks(chunk_size=1024):
f.write(chunk)
f.close()
return JsonResponse({\"flag\":\"succeed\"})
继续阅读与本文标签相同的文章
上一篇 :
买买买!在义外商今天都往这里赶
下一篇 :
ERP 软件需要MES系统功能的五个理由
-
学宏程序编程,这些知识必不可少!
2026-05-14栏目: 教程
-
华为准备卖出“落后”的5G,多家美企极力竞争!任正非格局太大!
2026-05-14栏目: 教程
-
百度:飞桨深度学习平台已累计服务150多万开发者
2026-05-14栏目: 教程
-
滴滴公布安全功能数据:近2亿用户添加紧急联系人
2026-05-14栏目: 教程
-
滴滴自动驾驶或将于年底落地上海
2026-05-14栏目: 教程
