【发布时间】: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 中将文件拆分成块,你应该是黄金