最近在項目中需要上傳excel文件到服務(wù)器。由于之前使用 httphelper 框架來發(fā)送 http 請求和服務(wù)端通信。
在嘗試使用 httphelper 發(fā)送文件的時候遇到些問題,耽誤了2天遲遲沒有解決。
最終嘗試使用 httpwebrequest 和 webclient 。
System.Net .WebClient Client = new System.Net .WebClient();
Client.Headers.Add("Content-Type", "binary/octet-stream");
byte[] result = Client.UploadFile("http://localhost/project1/upload.php", "POST", @"C:\img.jpg");
string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);