我使用 flask 和 flask-restplus 为我的应用程序生成一个上传 api 端点。一切似乎工作正常,但收到的文件数据为空,然后保存的文件为空。
这是我的代码
upload_parser = reqparse.RequestParser()
upload_parser.add_argument('data', location='files', type=FileStorage, required=True)
...
@ns.route('/upload')
class Upload(Resource):
@api.expect(upload_parser)
def post(self):
args = upload_parser.parse_args()
uploaded_file = args['data'] # This is FileStorage instance
scan_for_virus(uploaded_file) # This function raised error if a virus are found
destination = current_app.config.get('UPLOAD_DATA_FOLDER')
if not os.path.exists(destination):
os.makedirs(destination)
temp_filename = os.path.join(destination, str(uuid.uuid4()))
print uploaded_file # print "<FileStorage: u'IMG-20190129-WA0001.jpg' ('image/jpeg')>" seems correct
print uploaded_file.stream # print "<tempfile.SpooledTemporaryFile instance at 0x104b1c3f8>}"
print uploaded_file.content_length # print "0" ..... but my orignal file size is 4352436 bytes
uploaded_file.save(temp_filename) # create the file with the correct path, but this new file is empty.
return {'url': 'upload://{0}'.format(os.path.basename(temp_filename))}, 202
我使用swagger界面(由restplus框架生成)tu上传文件。发送的请求是:
curl -X POST "http://localhost:8888/api/upload" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "data=@my_file.pdf;type=application/pdf"
你有什么建议可以解决我的问题吗?我需要在我的烧瓶配置中指定一些特殊的东西吗?谢谢你的帮助
雷诺