文章 http://www.javashuo.com/article/p-plwlnljc-hb.html 裏面有提到服務端經過WebApi如何實現文件上傳,這裏就只說客戶端使用WebClient上傳,直接上代碼:html
private void button2_Click(object sender, EventArgs e) { try { string url = "http://localhost:29817/api/values/SaveFile"; if (string.IsNullOrEmpty(this.textBox1.Text)) { MessageBox.Show("請先選擇要上傳的文件"); return; } string fileName = this.textBox1.Text;//文件全路徑(e:\abc.txt) string safeFileName = Path.GetFileName(fileName);//文件名(abc.txt) WebClient client = new WebClient(); client.Credentials = CredentialCache.DefaultCredentials; client.Headers.Add("Content-Type", "application/form-data");//注意頭部必須是form-data client.QueryString["fname"] = safeFileName; byte[] fileb = client.UploadFile(new Uri(url), "POST", fileName); string res = Encoding.UTF8.GetString(fileb); MessageBox.Show(res); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
注意:web
1.Header的Content-Type必須設置爲application/form-dataapi
上面是同步方式上傳,下面是異步方式上傳 app
private void button2_Click(object sender, EventArgs e) { try { string url = "http://localhost:29817/api/values/SaveFile"; if (string.IsNullOrEmpty(this.textBox1.Text)) { MessageBox.Show("請先選擇要上傳的文件"); return; } string fileName = this.textBox1.Text;//文件全路徑(e:\abc.txt) string safeFileName = Path.GetFileName(fileName);//文件名(abc.txt) WebClient client = new WebClient(); //定義事件,上傳成功事件和上傳進度事件 client.UploadFileCompleted += client_UploadFileCompleted; client.UploadProgressChanged += client_UploadProgressChanged; client.Credentials = CredentialCache.DefaultCredentials; client.Headers.Add("Content-Type", "application/form-data");//注意頭部必須是form-data client.QueryString["fname"] = safeFileName; //異步上傳,經過事件回調獲取運行狀態 client.UploadFileAsync(new Uri(url), "POST", fileName); } catch (Exception ex) { MessageBox.Show("錯誤:"+ex.Message); } } //上傳進度事件,修改進度條 void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e) { progressBar1.Minimum = 0; progressBar1.Maximum = (int)e.TotalBytesToSend; progressBar1.Value = (int)e.BytesSent; progressBar1.Text = e.ProgressPercentage.ToString(); } //上傳完成事件,判斷是否上傳成功 void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e) { if (e.Error == null) { progressBar1.Value = progressBar1.Maximum; this.label2.Text = "上傳成功."; } else { MessageBox.Show("上傳錯誤:" + e.Error.Message); this.label2.Text = "上傳失敗."; } }
說明:異步
1.上傳的方法由UploadFile修改成UploadFileAsync,這個方法是異步的,非阻塞。this
2.註冊WebClient的兩個事件UploadFileCompleted和UploadProgressChanged,用於顯示進度。url
大文件傳輸spa
1.服務端web.config須要配置最大的請求:code
<configuration> <system.web> <httpRuntime targetFramework="4.5" maxRequestLength="2147483647" /> </system.web> <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="2147483647"/> </requestFiltering> </security> </system.webServer> </configuration>
2.WebApi代碼orm
[HttpPost]
public String SaveFile()
{
var request = HttpContext.Current.Request;
if (request.Files.Count > 0)
{
HttpPostedFile file= request.Files.Get(0);
file.SaveAs(Path.Combine("E:", file.FileName));
}
return "1";
}
備註:
1.通常配置爲最大2G差很少了,若是比2G還大,建議使用FTP上傳吧。