Flask FileStorage对象到File对象 [英] Flask FileStorage object to File Object
问题描述
我已经通过 flask
提交了一个api,它提交了一个文件,在后端收到它之后,我想用python <$进一步将它上传到一个未知的服务器c $ c> requests module
I have made an api through flask
which submit a file and after receiving it on the back end , I want to further upload this to an unknown server using python requests
module
如果我这样做,服务器接受该文件 -
The server accepts the file if I do this like -
requests.post(urlToUnknownServer,files={'file':open('pathToFile')})
现在我的要求是通过烧瓶上传文件参数。
在烧瓶中我得到的文件是 FileStorage
对象。我不想在我的服务器上保存它,而是直接希望它进一步上传。
所以基本上我想将 FileStorage
对象转换为返回类型 open()
函数是 file
(如果我错了请在这里纠正我)
Now my requirement is to get the file param uploaded through flask.
In flask I get the file as FileStorage
object. I don't want to save this on my server, instead directly wants it to upload further.
So basically I want to convert that FileStorage
object to return type of open()
function which is file
(please correct me here if I am wrong)
我曾尝试使用 -
obj=file(request.files['fileName'].read().encode('string-escape'))
requests.post(urlToUnknownServer,files={'file':obj})
这不起作用。可以在不保存我的服务器上的文件的情况下这样做。
This doesn't work. Can it be possible to do that without saving file on my server.
推荐答案
也许你可以使用read()而无需编码。例如:
Maybe you can use read() without encoding it. such as this:
obj=request.files['fileName'].read()
requests.post(urlToUnknownServer,files={'file':obj})
这篇关于Flask FileStorage对象到File对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!