django后台接收form-data 格式上传的文件
1,瀏覽器端端js程序
? ? ?瀏覽器以from-data表單的格式上傳文件。
?
?
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
?
轉(zhuǎn)載于:https://www.cnblogs.com/huanhuaqingfeng/p/11130284.html
總結(jié)
以上是生活随笔為你收集整理的django后台接收form-data 格式上传的文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虚拟机类加载机制---类加载器
- 下一篇: Web.sitemap网站导航