2014-05-08 33 views
3

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

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

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

我遇到的问题是我不知道如何访问从php发送UploadData数据。如果我在post数据上做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); 
    } 
} 

enter image description here

+0

我建议就如何分割下载成多个部分,并下载每件作品单独看例子:

而且还阅读。这里有一个关于将分割块与PHP结合起来的问题http://stackoverflow.com/questions/9011138/handling-pluploads-chunked-uploads-on-the-server-side所有你需要做的就是弄清楚如何分割文件从.NET的块,你应该是黄金 – user3036342

回答

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

但上传文件时,你应该在application/x-www-form-urlencoded代替使用multipart/form-data。另请参阅: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

0

使用此。这肯定会起作用。 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事情自动完成的。

+0

我已经想出了如何做到这一点,但正如我上面所说,我希望有能力发送文件的片段,我认为不可能与UploadFile。也许最好的解决方案是将我的文件分割成多个我想发送大小的文件,然后在每个文件上使用UploadFile?这似乎不像将内容读入内存和发送内容那样效率低下。 – Mike