C# 从WP7到Web Api的图像上载
我在网上看到很少有其他的例子做同样的事情,但我不知道为什么它对我不起作用 我创建了一个简单的WindowsPhone7应用程序,它使用PhotoChooserTask。 它使用Web Api将图像发送到服务器 以下是windows phone项目中的代码:C# 从WP7到Web Api的图像上载,c#,windows-phone-7,asp.net-web-api,C#,Windows Phone 7,Asp.net Web Api,我在网上看到很少有其他的例子做同样的事情,但我不知道为什么它对我不起作用 我创建了一个简单的WindowsPhone7应用程序,它使用PhotoChooserTask。 它使用Web Api将图像发送到服务器 以下是windows phone项目中的代码: void selectphoto_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) {
void selectphoto_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
var image = new Image();
image.Source = new BitmapImage(new Uri(e.OriginalFileName));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:59551/api/controllername");
request.Method = "POST";
request.ContentType = "multipart/form-data";
//private method to convert bitmap image to byte
byte[] str = BitmapToByte(image);
// Getting the request stream.
request.BeginGetRequestStream
(result =>
{
// Sending the request.
using (var requestStream = request.EndGetRequestStream(result))
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(str);
writer.Flush();
}
}
// Getting the response.
request.BeginGetResponse(responseResult =>
{
var webResponse = request.EndGetResponse(responseResult);
using (var responseStream = webResponse.GetResponseStream())
{
using (var streamReader = new StreamReader(responseStream))
{
string srresult = streamReader.ReadToEnd();
}
}
}, null);
}, null);
}
在Web API上,我获得了POST方法的以下代码:
public Task<HttpResponseMessage> Post()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
// Read the form data and return an async task.
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
}
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Image img = Image.FromFile(file.LocalFileName);
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
});
return task;
}
}
您发送的不是
multipart/form
。您只是发送一个字节流,它是应用程序/octet-stream
。只需在服务器上使用Request.Content.ReadAsStreamAsync()
,并将流复制到文件中 谢谢你,达雷尔。我已经按照你们的建议——并修改了代码——如我问题的编辑部分所示。我仍然无法接收图像数据。有什么建议吗?@gunnerz 1)尝试在请求正文上设置ContentLength。2) 尝试使用HttpClient而不是HttpWebRequest(需要WP7.5)3)尝试在服务器上使用async/await 4)尝试将Fiddler安装为代理,或使用Runscope调试请求。
public HttpResponseMessage Post()
{
var task = Request.Content.ReadAsStreamAsync();
task.Wait();
Stream requestStream = task.Result;
string root = HttpContext.Current.Server.MapPath("~/App_Data");
root = System.IO.Path.Combine(root, "xyz.jpg");
try
{
FileStream fs = System.IO.File.OpenWrite(root);
requestStream.CopyTo(fs);
fs.Close();
}
catch (Exception)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
}
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.Created;
return response;
}