我正在尝试将图片上传到媒体目录下的Images文件夹。我在views.py上使用了APIView;
class FileUploadView(APIView):
parser_classes = (FileUploadParser,)
def post(self, request, format='jpg'):
up_file = request.FILES['file']
destination = open('C:\\Users\\cano\\Desktop\\workspace\\campusBackend\\campusBackend\\media\\Images', 'wb+')
for chunk in up_file.chunks():
destination.write(chunk)
destination.close()
return Response(up_file.name, status.HTTP_201_CREATED)
但是,当我发送请求时,它会给我以下错误;
> PermissionError: [Errno 13] Permission denied:
> 'C:\\Users\\cano\\Desktop\\workspace\\campusBackend\\campusBackend\\media\\Images'
> [12/Dec/2016 15:10:45] "POST /imageUpload HTTP/1.1" 500 19170
我已经在settings.py中添加了MEDIA_ROOT和MEDIA_URL;
> MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
>
> MEDIA_URL = '/media/'
答案 0 :(得分:0)
试试这个:
destination = open('C:\\Users\\cano\\Desktop\\workspace\\campusBackend\\campusBackend\\media\\Images' + up_file.name, 'wb+')
使用您的代码,您尝试重写目录而不是文件。