【问题标题】:Uploading binary data from C# to PHP将二进制数据从 C# 上传到 PHP
【发布时间】:2014-05-08 09:07:02
【问题描述】:

我想将文件从 Windows C# 应用程序上传到运行 PHP 的 Web 服务器。

我知道 WebClient.UploadFile 方法,但我希望能够分块上传文件,以便我可以监控进度并能够暂停/恢复。

因此,我正在读取部分文件并使用 WebClient.UploadData 方法。

我遇到的问题是我不知道如何从 php.ini 访问使用 UploadData 发送的数据。如果我在发布数据上执行 print_r,我可以看到有二进制数据,但我不知道访问它的密钥。如何访问二进制数据?有没有更好的方法让我完全做到这一点?

String file = "C:\\Users\\Public\\uploadtest\\4.wmv";

using (BinaryReader b = new BinaryReader(File.Open(file, FileMode.Open)))
{
    int pos = 0;
    int required = 102400;
    b.BaseStream.Seek(pos, SeekOrigin.Begin);
    byte[] by = b.ReadBytes(required);

    using (WebClient wc = new WebClient()){
        wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        byte[] result = wc.UploadData("http://192.168.0.52/html/application.php", "POST", by);
        String s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
        MessageBox.Show(s);
    }
}

【问题讨论】:

  • 我建议查看有关如何将下载拆分为多个部分并单独下载每个部分的示例。这是一个关于将拆分块与 PHP 结合的 SO stackoverflow.com/questions/9011138/… 你所要做的就是弄清楚如何从 .NET 中将文件拆分成块,你应该是黄金

标签: c# php


【解决方案1】:

这就是我进行 HTTP 通信的方式。

我猜当我到达using() 时,HTTP 连接正在建立,并且在using() {...} 正文中您可以进行暂停和其他操作。

string valueString = "...";
string uriString = "http://someUrl/somePath";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uriString);
httpWebRequest.Method = "POST";
string postData = "key=" + Uri.EscapeDataString(valueString);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = byteArray.Length;
using (Stream dataStream = httpWebRequest.GetRequestStream())
{
    // do pausing and stuff here by using a while loop and changing byteArray.Length into the desired length of your chunks
    dataStream.Write(byteArray, 0, byteArray.Length);
}
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream receiveStream = httpWebResponse.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream);
string internalResponseString = readStream.ReadToEnd();

但是在上传文件时,您可能应该使用multipart/form-data 而不是application/x-www-form-urlencoded。另见:http://www.php.net/manual/en/features.file-upload.post-method.php

在 php 中,您可以使用超全局变量 $_FILES(例如 print_r($_FILES); )来访问上传的文件。

另请阅读:https://stackoverflow.com/a/20000831/1209443 了解如何处理 multipart/form-data 的更多信息

【讨论】:

    【解决方案2】:

    使用这个。这肯定会奏效。 System.Net.WebClient Client = new System.Net.WebClient();

        Client.Headers.Add("Content-Type", "binary/octet-stream");
        byte[] result = Client.UploadFile("http://192.168.0.52/mipzy/html/application.php", "POST", file);
        string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length); 
       MessageBox.Show(s);
    

    或者使用的那个

    using (System.Net.WebClient Client = new System.Net.WebClient()) 
    {
        Client.Headers.Add("Content-Type", "binary/octet-stream");
        byte[] result = Client.UploadFile("http://192.168.0.52/mipzy/html/application.php", "POST", file);
        string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length); 
        MessageBox.Show(s);
    }
    

    没有二进制阅读器。如果你想要二进制阅读器,你还必须为表单提供一些多部分参数。这是由 UploadFile 自动完成的。

    【讨论】:

    • 我已经想出了如何做到这一点,但正如我上面所说,我希望能够一次发送文件片段,我认为 UploadFile 不可能。也许最好的解决方案是将我的文件分成多个我想要发送的大小的文件,然后在每个文件上使用 UploadFile?与仅将片段读入内存并发送相比,这似乎不会非常低效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 2021-04-17
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2017-01-08
    相关资源
    最近更新 更多