上传文件中包含标题和边界 django Rest Framework

发布于 2025-01-19 20:01:47 字数 1305 浏览 0 评论 0 原文

我使用Django Rest框架将CSV文件上传到Django。我的所有工作都可以,但是文件内容中包含了来自请求标题的数据。

我期望这些标题已从文件内容中删除,因此文件看起来完全像上传的文件。

有什么想法吗?

...

django view

class FileUploadView(APIView):
    parser_classes = (FileUploadParser, )

    def post(self, request, format=None):    
        file = request.FILES['file']
        fs = FileSystemStorage()
        fs.save('output.csv', file)

input.csv:

Date,Customer,Team Member,Services
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
...

output.csv:

------WebKitFormBoundarycaNCidFSnOuN1u0G
Content-Disposition: form-data; name="file"; filename="input.csv"
Content-Type: text/csv

Date,Customer,Team Member,Services
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
...

------WebKitFormBoundarycaNCidFSnOuN1u0G--

http请求的相关标题:

Content-Disposition: attachment; filename=services.csv
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarycaNCidFSnOuN1u0G

I'm uploading a csv file to Django via ajax, using Django Rest Framework. I have everything working, but data from the request headers is included with the file content.

I was expecting these headers to have been stripped from the file content, so the file would look exactly like the uploaded file.

Any ideas?

...

Django View

class FileUploadView(APIView):
    parser_classes = (FileUploadParser, )

    def post(self, request, format=None):    
        file = request.FILES['file']
        fs = FileSystemStorage()
        fs.save('output.csv', file)

input.csv:

Date,Customer,Team Member,Services
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
...

output.csv:

------WebKitFormBoundarycaNCidFSnOuN1u0G
Content-Disposition: form-data; name="file"; filename="input.csv"
Content-Type: text/csv

Date,Customer,Team Member,Services
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
09-25-2018 03:30 PM,Suzy16 Neil16,Suzy,Men's Haircut | Legs
...

------WebKitFormBoundarycaNCidFSnOuN1u0G--

Relevant Headers From HTTP Request:

Content-Disposition: attachment; filename=services.csv
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarycaNCidFSnOuN1u0G

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2025-01-26 20:01:48

MultiPartParser 正是我所寻找的,因为前端将表单作为多部分表单发送。

class FileUploadView(APIView):
    parser_classes = (MultiPartParser,)
    ...

The MultiPartParser was what I was looking for as the front-end was sending up the form as a multipart form.

class FileUploadView(APIView):
    parser_classes = (MultiPartParser,)
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文