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