python - 通过 PUT 或 POST 为上传的文件添加了不必要的标头
问题描述
我用这个文档制作文件上传 APIhttps://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser
在view.py
class FileUploadView(APIView):
parser_classes = [FileUploadParser]
def put(self, request, filename, format=None):
file_obj = request.data['file']
entry = WaveFile(document=file_obj)
entry.save()
return Response(status=204)
然后在urls.py
urlpatterns += [
re_path(r'^upload/(?P<filename>[^/]+)$', FileUploadView.as_view())
]
模型如下所示
class WaveFile(models.Model):
description = models.CharField(max_length=255, blank=True)
document = models.FileField(upload_to='@_mat/_spleeter_wav/')
uploaded_at = models.DateTimeField(auto_now_add=True)
现在我可以PUT
像这样发送文件curl -X PUT -F upfile=_mat/e_h_01.jpg http://localhost:8008/upload/e_h_01.jpg
但是上传的文件被破坏了,因为文件附加了不必要的文件头。
--------------------------02d173694176f7aa
Content-Disposition: form-data; name="upfile"; filename="e_h_01.jpg"
Content-Type: image/jpeg
为什么会这样?如何从中提取文件本身request.data['file']
?
解决方案
推荐阅读
- angular - 离子可选组件,选定值未显示在控制台日志中
- angular - 尽管添加了 .d.ts 文件,但类型声明错误
- objective-c - 将 NSBitmapImageRep 转换为 NSImage
- mysql - 将字符集从 latin2_bin 更改为 utf8 并删除所有以“特殊”字符开头的数据
- python - 来自条目文本字段的 Tkinter get()
- php - 在使用 Eloquent 上传到数据库之前检查模型属性是否已经存在,性能问题
- angular - Ionic 项目仅在使用 --prod 构建时引发错误
- sql - 此代码中的 IF 语句有什么问题?
- vuetify.js - 图像未从 vuetify 上 v-img 上的 url 加载
- .net - 使用 axios.post 发送防伪令牌时出现 400 错误