I am trying to load a StorageFile (mainly image files) into a PHP file so that it can save it to the server.
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 problem is that when I try to download the file, it gave me an error The object has been closed. (Exception from HRESULT: 0X80000013)and Exception thrown: 'System.ObjectDisposedException' in mscorlib.ni.dll.. I don’t understand, I upload the file inside the instruction using, how can this be located? Am I doing something wrong?
Debugging shows me this
: : POST, RequestUri: 'http://localhost/uploadFile.php', : Windows.Web.Http.HttpMultipartFormDataContent, TransportInformation: ServerCertificate: '', ServerCertificateErrorSeverity: None, ServerCertificateErrors: {}, ServerIntermediateCertificates: {}, : {Accept-Encoding: gzip, deflate} {Content-Length: 27749, Content-Type: multipart/form-data; border = 9955f08b-e82d-428b-82e1-3197e5011ccd}
: StatusCode: 200, ReasonPhrase: 'OK', : 2, : Windows.Web.Http.HttpStreamContent, : {: Keep-Alive, : Apache/2.4.18 (Ubuntu), Keep -Alive: timeout = 5, max = 100, Date: Sun, 06 Nov 2016 04:02:40 GMT} {Content-Length: 28, Content-Type: text/html; = UTF-8}