浏览器上传文件+django后台处理
Posted huanhuaqingfeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浏览器上传文件+django后台处理相关的知识,希望对你有一定的参考价值。
1,浏览器端端js程序
2,django后端处理程序
接收来自浏览器上传的文件,并把文件存储在一指定的路径下。
product_id = request.POST.get(‘product_id‘)
sign = request.POST.get(‘sign‘)
file_size = request.POST.get(‘file_size‘)
sw_ver = request.POST.get(‘sw_ver‘)
file_dict = request.FILES.items()
if not file_dict:
dict[‘status‘] = ‘no file upload‘
return dict
for (k, v) in file_dict:
logger.info("dic[%s]=%s", k, v)
file_data = request.FILES.getlist(k)
for fl in file_data:
filename = fl._get_name()
logger.info("filename=%s", filename)
path_file=base_path+"upgrade/"+product_id+‘/‘
logger.info("path_file=%s", path_file)
if not os.path.exists(path_file):
os.makedirs(path_file)
logger.info("path_file=%s", path_file)
path_file+=filename
# logger.info( "path_file1=%s",path_file1)
# path_file=path_file1.encode(‘gb2312‘)
logger.info("path_file=%s", path_file)
try:
with open(path_file, "wb") as f:
if fl.multiple_chunks():
logger.info("multiple_chunks")
for content in fl.chunks():
f.write(content)
else:
data=fl.read() ###.decode(‘utf-8‘)
logger.info("not multiple_chunks=%s", data)
f.write(data)
#except Exception as e:
except:
#logger.info("error=%s", repr(e))
logger.info("file write fail")
dict[‘status‘] = ‘file write fail‘
return dict
以上是关于浏览器上传文件+django后台处理的主要内容,如果未能解决你的问题,请参考以下文章
Django(十九)文件上传:图片上传(后台上传自定义上传)
ASP.NET Core MVC(C#) 文件上传及后台输出响应Aspose.Words处理后文件
Linux下开发python django程序(设置admin后台管理上传文件)