我想將文件從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);
}
}
我建議就如何分割下載成多個部分,並下載每件作品單獨看例子:
而且還閱讀。這裏有一個關於將分割塊與PHP結合起來的問題http://stackoverflow.com/questions/9011138/handling-pluploads-chunked-uploads-on-the-server-side所有你需要做的就是弄清楚如何分割文件從.NET的塊,你應該是黃金 – user3036342