如何使用django rest框架上传和保存.zip文件

时间:2016-06-30 13:53:22

标签: python django python-2.7 django-rest-framework django-cors-headers

我必须上传一个.zip文件并使用Django rest框架将其保存到服务器中的某个位置。我的代码是

`class upload_file(APIView):
    parser_classes = (FileUploadParser,)
    def post(self, request, filename, format=None):
        file_obj = request.data['file']
        destination = open('C:\\Temp\\' + filename, 'wb+')
        for chunk in file_obj.chunks():
            print(chunk)
            destination.write(chunk)
            destination.close()
        return Response({"status":status.HTTP_204_NO_CONTENT, "data":file_obj})`    

它创建一个带有filename的.zip文件,其中包含另一个相同的.zip文件。提取内部zip包显示C:\Temp\files_backups\files_backups.zip: The archive is either in unknown format or damaged消息 我试过这个时

`class upload_file(APIView):
    parser_classes = (FileUploadParser,)
    def post(self, request, filename, format=None):
        file_obj = request.data['file']
        with open('C:\\Temp', 'wb+') as temp_file:
            for chunk in file_obj.chunks():
                temp_file.write(chunk)
        return Response({"status":status.HTTP_204_NO_CONTENT, "data":file_obj})`    

显示exceptions.IOError: [Errno 13] Permission denied: 'C:\\Temp' 我经历了很多解决方案,但没有找到任何合适的解决方案。任何人都可以指导如何上传.zip文件并将其保存到C:\Temp文件夹?

提前致谢。

0 个答案:

没有答案