700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > django后台接收form-data 格式上传的文件

django后台接收form-data 格式上传的文件

时间:2021-11-28 05:24:27

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。