我一直在尝试将StorageFile(主要是图像文件)上传到PHP文件,以便它可以保存到服务器中。
public async Task<bool> uploadFile(StorageFile file)
{
try
{
using (HttpMultipartFormDataContent form = new HttpMultipartFormDataContent())
{
using (IInputStream fileStream = await file.OpenSequentialReadAsync())
{
HttpStreamContent streamContent = new HttpStreamContent(fileStream);
form.Add(streamContent, "file", file.Name);
using (HttpClient client = new HttpClient())
{
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("localhost/uploadFile.php")))
{
request.Content = form;
HttpResponseMessage response = await client.SendRequestAsync(request);
Debug.WriteLine("\nRequest: " + request.ToString());
Debug.WriteLine("\n\nResponse: " + response.ToString());
}
}
}
}
return true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
return false;
}
}
<?php
$uploaddir = 'uploads/';
$uploadedFile = $uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadedFile)){
echo 'File upload success!';
} else {
echo 'Possible file upload attack!';
}
?>
问题是,当我尝试上传文件时,它向我发送了一个错误The object has been closed. (Exception from HRESULT: 0X80000013)
和Exception thrown: 'System.ObjectDisposedException' in mscorlib.ni.dll
..我不明白,我在{{1}内部上传文件声明,怎么处理?我做错了吗?
调试向我展示了这个
请求:方法:POST,RequestUri:&#39; http://localhost/uploadFile.php&#39;,内容:Windows.Web.Http.HttpMultipartFormDataContent,TransportInformation:ServerCertificate:&#39;&#39;,ServerCertificateErrorSeverity:无,ServerCertificateErrors:{},ServerIntermediateCertificates:{},标题:{Accept-Encoding:gzip,deflate} {Content-Length:27749,Content-Type:multipart / form-data; boundary = 9955f08b-e82d-428b-82e1-3197e5011ccd}
响应:StatusCode:200,ReasonPhrase:&#39; OK&#39;,版本:2,内容:Windows.Web.Http.HttpStreamContent,Headers:{Connection:Keep-Alive,Server:Apache / 2.4.18 (Ubuntu),Keep-Alive:timeout = 5,max = 100,Date:Sun,2016年11月6日04:02:40 GMT} {Content-Length:28,Content-Type:text / html;字符集= UTF-8}
答案 0 :(得分:2)
Using statement提供了一种方便的语法,可确保正确使用IDisposable对象。简单地说,它可以帮助您执行Dispose()
方法。所以你的代码等于:
HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();
IInputStream fileStream = await file.OpenSequentialReadAsync();
HttpStreamContent streamContent = new HttpStreamContent(fileStream);
form.Add(streamContent, "file", file.Name);
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("http://127.0.0.1:9096/hello.php"));
request.Content = form;
HttpResponseMessage response = await client.SendRequestAsync(request);
Debug.WriteLine("\nRequest: " + request.ToString());
Debug.WriteLine("\n\nResponse: " + response.ToString());
request.Dispose();
client.Dispose();
fileStream.Dispose();
form.Dispose();
您将在form.Dispose();
代码行获得例外。原因是HttpMultipartFormDataContent.Add方法不需要处理。在我看来,并非需要处理的非托管资源,HttpMultipartFormDataContent
类ReadAsBufferAsync
之类的其他方法可能需要处理。
按如下方式更新您的代码,这不会引发closed
异常:
HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();
using (IInputStream fileStream = await file.OpenSequentialReadAsync())
{
HttpStreamContent streamContent = new HttpStreamContent(fileStream);
form.Add(streamContent, "file", file.Name);
using (HttpClient client = new HttpClient())
{
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("http://127.0.0.1:9096/hello.php")))
{
request.Content = form;
HttpResponseMessage response = await client.SendRequestAsync(request);
Debug.WriteLine("\nRequest: " + request.ToString());
Debug.WriteLine("\n\nResponse: " + response.ToString());
}
}
}