首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Falcon和falcon-multipart + POST上传文件请求实现

Falcon和falcon-multipart + POST上传文件请求实现
EN

Stack Overflow用户
提问于 2018-02-23 11:12:35
回答 2查看 5.7K关注 0票数 5

我正在尝试用Falcon框架(python)实现上传文件的POST请求。

我使用了falcon-multipart来实现多部分/格式数据,这允许我在cgi.FieldStorage()中检索我的文件,其中的文件是二进制格式的,但是现在,我需要将这个文件写在一个具有原始扩展名的目录中。

这就是我使用的代码。

app.py:

代码语言:javascript
复制
import falcon

from .files import Resource

from falcon_multipart.middleware import MultipartMiddleware

api = application = falcon.API(middleware=[MultipartMiddleware()])

files = Resource()
api.add_route('/files', files)

files.py:

代码语言:javascript
复制
import io
import os
import shutil

import falcon
import json


class Resource(object):

   _storage_path = './uploaded_files'

   def on_post(self, req, resp):
        """
        POST METHOD
        """
        # Retrieve file extension
        ext = req.get_param('extension')

        # Retrieve input_file
        input_file = req.get_param('file')

        # Read file as binary
        raw = input_file.file.read()

        # Retrieve filename
        filename = input_file.filename

        # Define file_path
        file_path = os.path.join(self._storage_path, filename)

        # Write to a temporary file to prevent incomplete files from
        # being used.
        temp_file_path = file_path + '~'

        # Finally write the data to a temporary file
        with open(temp_file_path, 'wb') as output_file:
            shutil.copyfileobj(raw, output_file)

        # Now that we know the file has been fully saved to disk
        # move it into place.
        os.rename(temp_file_path, file_path)

        resp.status = falcon.HTTP_201
EN

回答 2

Stack Overflow用户

发布于 2018-02-24 01:03:55

我不得不学习cgi

  • cgi - File upload
  • cgi - Big file upload

这是我使用的实现:

代码语言:javascript
复制
def on_post(self, req, resp):
        """
        POST METHOD
        """
        # Retrieve input_file
        input_file = req.get_param('file')

        # Test if the file was uploaded
        if input_file.filename:
            # Retrieve filename
            filename = input_file.filename

            # Define file_path
            file_path = os.path.join(self._storage_path, filename)

            # Write to a temporary file to prevent incomplete files
            # from being used.
            temp_file_path = file_path + '~'

            open(temp_file_path, 'wb').write(input_file.file.read())

            # Now that we know the file has been fully saved to disk
            # move it into place.
            os.rename(temp_file_path, file_path)

    resp.status = falcon.HTTP_201
票数 7
EN

Stack Overflow用户

发布于 2018-04-10 14:53:33

尝尝这个

代码语言:javascript
复制
import io
import os
import uuid
import mimetypes
import falcon
import json




class Resource(object):

        _CHUNK_SIZE_BYTES = 4096

        def __init__(self, storage_path):
            self._storage_path = storage_path

        def on_post(self, req, resp):
            image = req.get_param("profilePic")
            # image_type = req.get_param("profilePic").type        
            ext = mimetypes.guess_extension(req.content_type)
            filename = "{uuid}{ext}".format(uuid=uuid.uuid4(), ext=ext)
            image_path = os.path.join(self._storage_path, filename)
            with open(image_path, "wb") as image_file:
                while True:
                    chunk = image.file.read(4096)
                    image_file.write(chunk)
                    if not chunk:
                        break
            resp.status = falcon.HTTP_200
            resp.location = filename
            resp.body = json.dumps("{name:" + image_path + "}")


import falcon
from falcon_multipart.middleware import MultipartMiddleware    

api = application = falcon.API(middleware=[MultipartMiddleware()])

images = Resource('images')
api.add_route('/images', images)`
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48940530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档