FileUploadParser未获取文件名

时间:2017-10-18 08:57:25

标签: django django-rest-framework

我只想创建一个REST API来接收文件,处理它并返回一些信息。我的问题是我遵循这个例子: http://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser

我无法使用Postman或curl使其工作,我想我错过了一些东西。解析器总是给我这两个错误:

  • FileUpload解析错误 - 上传处理程序都无法处理流
  • 缺少文件名。请求应包含带有filename参数的Content-Disposition标头。

这是代码:

views.py:

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

    def post(self, request, filename, format=None):
        file_obj = request.data['file']
        # ...
        # do some stuff with uploaded file
        # ...
        return Response(status=204)

    def put(self, request, filename, format=None):
        file_obj = request.data['file']
        # ...
        # do some stuff with uploaded file
        # ...
        return Response(status=204)

urls.py

urlpatterns = [
   url(r'predict/(?P<filename>[^/]+)$', app.views.FileUploadView.as_view())
]

settings.py

"""
Django settings for GenderAPI project.

Generated by 'django-admin startproject' using Django 1.9.1.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os
import posixpath


LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = removed

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['localhost','127.0.0.1']

REST_FRAMEWORK = {
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.FileUploadParser'
    )
}


# Application definition

INSTALLED_APPS = [    

    # Add your apps here to enable them
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',   
    'rest_framework',
    'app'  


]

MIDDLEWARE = [

    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware'
]

ROOT_URLCONF = 'GenderAPI.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'GenderAPI.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static']))

FILE_UPLOAD_TEMP_DIR = BASE_DIR
MEDIA_URL  = '/media/'

在这里你可以看到一个邮差捕获(我已经尝试了一切):

PUT /predict/pabloGrande.jpg HTTP/1.1
Host: 127.0.0.1:52276
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="04320cf.jpg"
Content-Type: image/jpeg


------WebKitFormBoundary7MA4YWxkTrZu0gW--

要求:

bleach==1.5.0
Django==1.11.6
djangorestframework==3.7.1
html5lib==0.9999999
Markdown==2.6.9
numpy==1.13.3
olefile==0.44
pandas==0.20.3
Pillow==4.3.0
pip==9.0.1
protobuf==3.4.0
python-dateutil==2.6.1
pytz==2017.2
scipy==1.0.0rc1
setuptools==28.8.0
six==1.11.0
tensorflow==1.3.0
tensorflow-tensorboard==0.1.8
Werkzeug==0.12.2
wheel==0.30.0

非常感谢你的帮助

6 个答案:

答案 0 :(得分:11)

在django REST框架中。我们有像Parsers,Renderers和Serializers这样的组件。

  • Parsers的职责是解析请求方法GET,POST和PUT等发送的数据。

  • django REST中使用的默认解析器是“JSONParser”。它只解析数据JSON数据[数字,字符串,日期]。它忽略了像FILES这样的数据。

  • 为了解析FILES,我们需要使用“MultiPartParser”或“FormParser”等解析器。

    示例代码:

        from rest_framework.parsers import MultiPartParser
        from rest_framework.response import Response
        from rest_framework.views import APIView
    
        class ExampleView(APIView):
            """
            A view that can accept POST requests with JSON content.
            """
            parser_classes = (MultiPartParser,)
    
            def post(self, request, format=None):
                # to access files
                print request.FILES
                # to access data
                print request.data
                return Response({'received data': request.data})
    

当我们使用属性request.data时,解析器将解析数据。

参考文献:Django REST DocsDjango REST Github

答案 1 :(得分:1)

你的views.py中的

更改了像这样的解析器

parser_classes = (JSONParser, MultiPartParser)

答案 2 :(得分:1)

在使用邮递员的第二个错误Missing filename. Request should include a Content-Disposition header with a filename parameter.上,我删除了标头Content-Type :multipart/form-data并成功了。

似乎问题是自定义Content-Type标头会覆盖应发送的默认Content-Type标头。请查看此主题以获取参考Content-Type for multipart posts

答案 3 :(得分:1)

我遇到同样的问题。问题错误消息显示:

  

{“ detail”:“缺少文件名。请求应包含一个   具有文件名参数的Content-Disposition标头。“}   我已完成答案上方的所有步骤,但没有用。最后,

我发现原因是在视图集的后端。

它像这样显示

parser_classes = (FileUploadParser, MultiPartParser, FormParser)

然后删除FileUploadParser

  parser_classes = ( MultiPartParser, FormParser)

而且有效,所以我认为您应该更加注意

答案 4 :(得分:0)

您不需要使用MultipartParserFormParser

您需要的是一个带有FileField()的序列化器,如下所示:

serializers.py:

class FileUploadSerializer(serializers.Serializer):
    # I set use_url to False so I don't need to pass file 
    # through the url itself - defaults to True if you need it
    file = serializers.FileField(use_url=False)

因此,当您尝试在下面访问file时,将有一个字典,其键名为file。就个人而言,我可能会说它比“文件”更具描述性,但这取决于您。

from .serializers import FileUploadSerializer

    class FileUploadView(APIView):

       def post(self, request):
           # set 'data' so that you can use 'is_vaid()' and raise exception
           # if the file fails validation
           serializer = FileUploadSerializer(data=request.data)
           serializer.is_valid(raise_exception=True)
           # once validated, grab the file from the request itself
           file = request.FILES['file']

答案 5 :(得分:0)

添加

Content-Disposition: attachment; filename=<insert-your-file-name>

解决了标题问题,然后在view方法中以以下方式访问此文件:

file = self.request.FILES['file']