嘿,所以我有点问题。我写了一个 django restframework api 用于将文件上传到我的本地目录。对于 pdf,它似乎工作得很好,但任何其他类型的格式都会损坏文件并使其无法打开。
(这包括 png/jpg/任何其他图片格式、txt 文件、xlsx 文件等)这些文件都保存在正确的路径中,它们的命名正确无误。
class UploadInvoiceFile(APIView):
parser_classes = (FileUploadParser, MultiPartParser)
def put(self, request, filename, specific_path='admin'):
file_obj = request.data['file']
file_path = settings.INVOICE_URL[admin]
file = file_path+'/'+filename
if not os.path.exists(file_path):
os.makedirs(file_path)
with open(file, 'wb+') as destination:
for chunk in file_obj.chunks():
destination.write(chunk)
return Response(status=204)
Run Code Online (Sandbox Code Playgroud)
更新:我发现被压缩的文件保存了额外的东西
------WebKitFormBoundaryKDALl9LeBZb6xbOo 内容配置:表单数据;名称=“文件”;filename="123.txt" 内容类型:文本/纯文本
档案资料
------WebKitFormBoundaryKDALl9LeBZb6xbOo--