我一直在尝试上传一个StorageFile(主要是图像文件)到一个PHP文件,以便它可以保存到服务器中。将StorageFile上传到PHP文件
ViewModel.cs
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;
}
}
uploadFile.php
<?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
..我不明白,我在using
声明里面做文件上传,怎么处理?难道我做错了什么?
调试表明我这个
请求:方法:POST,RequestUri: 'http://localhost/uploadFile.php',内容:Windows.Web.Http.HttpMultipartFormDataContent,TransportInformation:ServerCertificate: '',ServerCertificateErrorSeverity:无,ServerCertificateErrors: {},ServerIntermediateCertificates:{},Headers:{Accept-Encoding:gzip,deflate} {Content-Length:27749,Content-Type:multipart/form-data;边界= 9955f08b-e82d-428b-82e1-3197e5011ccd}
响应:状态码:200,ReasonPhrase:'OK',版本:2,内容:Windows.Web.Http.HttpStreamContent,标题:{连接: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; charset = UTF-8}