【发布时间】:2019-01-27 23:46:37
【问题描述】:
我正在尝试使用 C# 构建 UWP 应用程序。而且我还在我的其他设备(即树莓派)上使用烧瓶构建了一个宁静的网络 api。但是当我尝试通过 api 将 UWP 应用程序中的图像上传到树莓派时,树莓派在请求似乎成功时没有收到文件。
所以,在 raspberry pi 上运行 web api,然后在 windows 10 上运行 uwp 应用程序后,我得到了这个返回:“no file”。
这是我的 UWP 应用的代码
public async void Upload_FileAsync(string WebServiceURL, string
FilePathToUpload){
IStorageFile storageFile = await
StorageFile.GetFileFromPathAsync(FilePathToUpload);
IRandomAccessStream stream = await
storageFile.OpenAsync(FileAccessMode.Read);
HttpStreamContent streamfile = new HttpStreamContent(stream);
HttpMultipartFormDataContent httpContents = new
HttpMultipartFormDataContent();
httpContents.Headers.ContentType.MediaType = "multipart/form-data";
httpContents.Add(streamfile, "file");
var client = new HttpClient();
HttpResponseMessage result = await client.PostAsync(
new Uri(WebServiceURL), httpContents);
string stringReadResult = await result.Content.ReadAsStringAsync();
textBox.Text = stringReadResult;
}
这就是我调用函数的方式
Upload_FileAsync("http://192.168.0.111:5000/upload",
"c:\\pictures\\testImage3.jpg");
这是rest api的代码
from flask import Flask
from flask_restful import Resource, Api, reqparse
import werkzeug, os
app = Flask(__name__)
api = Api(app)
UPLOAD_FOLDER = 'static/img'
parser = reqparse.RequestParser()
parser.add_argument('file',
type=werkzeug.datastructures.FileStorage,
location='files')
class PhotoUpload(Resource):
def post(self):
data = parser.parse_args()
if data['file'] == None:
return "no file"
photo = data['file']
if photo:
filename = 'received.png'
photo.save(os.path.join(UPLOAD_FOLDER, filename))
return "file uploaded"
api.add_resource(PhotoUpload, '/upload')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
我期待的是:通过api成功上传一张图片到树莓派,并存储在树莓派上。但实际输出的是“无文件”。
树莓派上的打印输出是这样的:
[27/Jan/2019 17:18:02] "POST /upload HTTP/1.1" 200 -
所以看起来请求很好,但文件不在请求中。
【问题讨论】:
-
您是否检查过 UWP 所需的网络功能?为此,请双击项目中的 package.appxmanifest 文件并转到“功能”选项卡。
-
是的,我已经检查过了,选择了私有网络(客户端和服务器)
-
我认为这肯定是在客户端,因为以下 curl 命令以当前形式适用于您的烧瓶代码:
curl -i -X POST -F 'file=@README.md' "http://localhost:5000/upload" -H 'ContentType: multipart/form-data' -
我也遇到过这种情况,但我不知道 UWP 代码出了什么问题。