提问者:小点点

将存储文件上传到 PHP 文件


我一直在尝试将存储文件(主要是图像文件)上传到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!';
}
?>

问题是当我尝试上传文件时,它给我抛出了一个错误对象已关闭。(HRESULT的例外:0X80000013)抛出的异常:mscorlib.ni.dll.中的“System.ObjectDisposedException”。我不明白,我在 using 语句内上传文件,如何处置?我做错了什么吗?

调试向我展示了这个

请求: 方法: POST, 请求URI: 'http://localhost/uploadFile.php', 内容: Windows.Web.Http.Http.HttpMultipartFormDataContent, 传输信息: ServerCertificate: '', ServerCertificateErrorSernity: None, ServerCertificateErrors: {}, ServerIntermediateCertificate:{}, headers:{ Accept-Encoding: gzip, deflate }{ Content-Length: 27749, Content-Type: multipart/form-data; boundary=9955f08b-e82d-428b-82e1-3197e5011ccd }

响应: 状态代码: 200, 原因短语: 'OK', 版本: 2, 内容: Windows.Web.Http.HttpStreamContent, 标头:{连接: 保持活动, 服务器: Apache/2.4.18 (Ubuntu), 保持活动: 超时=5, 最大=100, 日期: 星期日, 06 十一月 2016 04:02:40 GMT}{ 内容长度: 28, 内容类型: 文本/html; 字符集=UTF-8}


共1个答案

匿名用户

using 语句提供了一种方便的语法,可确保正确使用 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();

您将在表单上获得例外。处置();代码行。原因是 HttpMultipartFormDataContent.Add 方法不需要释放。在我看来,不是不需要处置的非托管资源,HttpMultipartFormDataContent 类的其他方法(如 ReadAsBufferAsync)可能需要处置。

按如下方式更新代码,这不会引发已关闭的异常:

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());
        }
    }
}