我一直在嘗試上傳一個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}