简体   繁体   English

Python Flask Uploading FIles

[英]Python Flask Uploading FIles

I'm trying to upload user selected image into my firebase. 我正在尝试将用户选择的图像上传到我的firebase中。

When I browse for the file 当我浏览文件时

file = request.files['inputFile']

and I try this 我试试这个

storage.child("images/examples.jpg").put(file)

I get an error 我收到一个错误

io.UnsupportedOperation: fileno

How do I go about fixing this? 我该如何解决这个问题? I just want user to select the file and I be able to make use of the .jpg file and upload it 我只是想让用户选择文件,我可以使用.jpg文件并上传它

The put method takes a path to a local file (and an optional user token). put方法采用本地文件(和可选的用户令牌)的路径。

request.files[key] returns a custom object that represents the uploaded file. request.files[key]返回表示上传文件的自定义对象。 Flask documentation links: file uploads quickstart , incoming request data api , FileStorage class . Flask文档链接: 文件上传快速入门传入请求数据apiFileStorage类

You need to store the uploaded file data to a local file, and the pass that file name to the put method: 您需要将上载的文件数据存储到本地文件,并将该文件名传递给put方法:

request.files['inputFile'].save("some_filename.ext")
storage.child("images/examples.jpg").put("some_filename.ext")

Look into the tempfile module to generate random temporary file names (instead of using the hard coded some_filename.ext , which obviously is not a very good idea with concurrent requests). 查看tempfile模块以生成随机临时文件名(而不是使用硬编码的some_filename.ext ,这对于并发请求显然不是一个好主意)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM