【发布时间】:2021-05-13 01:22:42
【问题描述】:
如果我正确处理请求,我不明白,上传所有文件后都是 1 KB,我无法打开它们。如何创建正确的文件?如果我将文件另存为 .doc,我可以看到:
------WebKitFormBoundaryt3UjlK5SVq8hgppA
Content-Disposition: form-data; name="file"
[object FileList]
------WebKitFormBoundaryt3UjlK5SVq8hgppA--
所以我的函数要在 js 文件中提交:
async submitFiles() {
let formData = new FormData();
formData.append('file', this.file);
console.log(this.file)
axios.put(`/api/v1/myapp/upload/${this.file[0].name}`,
formData,
{
headers: {
'Content-Disposition': 'attachment',
'X-CSRFToken': await this.getCsrfToken(),
},
}
).then(function () {
console.log('SUCCESS!!');
})
.catch(function () {
console.log('FAILURE!!');
});
},
处理表单文件的变化
fileChanged(file) {
this.file = file.target.files
},
最后是我的 view.py
class FileUploadView(APIView):
parser_classes = [FileUploadParser]
def put(self, request, filename, format=None):
file_obj = request.data['file']
handle_uploaded_file(file_obj)
return Response({'received data': request.data})
在哪里
def handle_uploaded_file(f):
with open('path/to/my/folder/' + str(f.name), 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
【问题讨论】:
标签: javascript python-3.x django django-rest-framework vuejs3