最初,我使用的是Windows Phone 8System.Net.Http
库将图像从SD卡上载到Python服务器,代码如下:
private async void UploadFile()
{
try
{
// Make sure there is a picture selected
if (photoStream != null)
{
// initialize the client
// need to make sure the server accepts network IP-based
// requests.
// ensure correct IP and correct port address
var fileUploadUrl = @"http://IPAddress/";
var client = new HttpClient();
// Reset the photoStream position
// If you don't reset the position, the content lenght
// sent will be 0
photoStream.Position = 0;
// This is the postdata
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new StreamContent(photoStream), "fn", fileName);
// upload the file sending the form info and ensure a result.
// it will throw an exception if the service doesn't return
// a valid successful status code
await client.PostAsync(fileUploadUrl, content)
.ContinueWith((postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
}
// Disable the Upload button
btnUpload.IsEnabled = false;
// reset the image control
DisplayImage.Source = null;
}
catch
{
}
}
在服务器端,我使用以下python代码对请求进行解码:
^{pr2}$在我从WindowsPhone8切换到WindowsPhone8.1Silverlight之前,它一直有效。由于Windows Phone 8.1 Silverlight不再支持System.Net.Http
库,因此我使用了Windows.Web.Http
,并修改了代码:
private async void UploadFile()
{
try
{
// Make sure there is a picture selected
if (file != null)
{
// initialize the client
// need to make sure the server accepts network IP-based
// requests.
// ensure correct IP and correct port address
var fileUploadUrl = new Uri(@"http://IPAddress/", UriKind.Absolute);
var client = new HttpClient();
HttpMultipartFormDataContent content = new HttpMultipartFormDataContent();
HttpStreamContent streamContent = new HttpStreamContent(photoStream);
content.Add(streamContent, "fn", fileName);
await client.PostAsync(fileUploadUrl, content);
}
// Disable the Upload button
btnUpload.IsEnabled = false;
// reset the image control
DisplayImage.Source = null;
}
catch
{
}
}
服务器端保持不变。但是,上传的结果文件现在会定期插入额外的字节,看起来像'\r\n10000\r\n'
或{pdict
将边界映射到类似于xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
的内容,但这似乎与通过System.Net.Http
上载文件时的行为相同。
我尝试在一个较小的文件中手动删除这些字节,该文件似乎可以确认该文件在其他方面是正确的。在
为什么会出现这些额外的字节?这是Python服务器错误处理POST请求的问题吗?在
以下是我遇到这个问题时的解决方案:
客户:
控制器:
^{pr2}$相关问题 更多 >
编程相关推荐