2014-05-08 36 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