解码从 POST 请求收到的 werkzeug.datastructures.ImmutableMultiDict 的图像
Decode image from werkzeug.datastructures.ImmutableMultiDict received from POST request
我正在尝试通过 C# 中的 HTTP POST 请求发送图像,以供使用 Flask 用 Python 编写的 API 读取。在 c# 前端,我用来发送图像的代码是:
WebClient wc = new WebClient();
wc.UploadFileAsync(new Uri("http://172.30.211.33:5000/post_data"), "POST", StringPhotoPath);
此代码似乎有效;在 API 端,我收到一条消息,指出数据已发布。我在API端使用的方法如下:
@app.route('/post_data',methods=['POST'])
def post_data():
global stored_data
#maybe expand to other MIME types
stored_data=request.files
print('data posted')
return "complete"
当我在上传后查看存储的数据时,这是输出:
ImmutableMultiDict([('file', <FileStorage: '9e6df298ff964b9c88df9f9634aa9e6d.jpg' ('application/octet-stream')>)])
这很好,只是每次尝试读取它都会导致
ValueError: I/O operation on closed file.
如何从中提取图像对象?
好的,我找到了解决办法;您必须将返回的文件保存到一个临时目录,因为文件存储对象确实在 POST 操作完成后关闭,无法打开它。我使用的代码如下:
global stored_filepath
file=request.files['file']
file.save('filepath'+file.filename)
#for later use
stored_filepath='C:\Users\rdp\Documents\GitHub\Blueline-Python-Scripts\production scripts\data_files\'+file.filename
我正在尝试通过 C# 中的 HTTP POST 请求发送图像,以供使用 Flask 用 Python 编写的 API 读取。在 c# 前端,我用来发送图像的代码是:
WebClient wc = new WebClient();
wc.UploadFileAsync(new Uri("http://172.30.211.33:5000/post_data"), "POST", StringPhotoPath);
此代码似乎有效;在 API 端,我收到一条消息,指出数据已发布。我在API端使用的方法如下:
@app.route('/post_data',methods=['POST'])
def post_data():
global stored_data
#maybe expand to other MIME types
stored_data=request.files
print('data posted')
return "complete"
当我在上传后查看存储的数据时,这是输出:
ImmutableMultiDict([('file', <FileStorage: '9e6df298ff964b9c88df9f9634aa9e6d.jpg' ('application/octet-stream')>)])
这很好,只是每次尝试读取它都会导致
ValueError: I/O operation on closed file.
如何从中提取图像对象?
好的,我找到了解决办法;您必须将返回的文件保存到一个临时目录,因为文件存储对象确实在 POST 操作完成后关闭,无法打开它。我使用的代码如下:
global stored_filepath
file=request.files['file']
file.save('filepath'+file.filename)
#for later use
stored_filepath='C:\Users\rdp\Documents\GitHub\Blueline-Python-Scripts\production scripts\data_files\'+file.filename