前台代碼:
<form id="form1" action="http://www.abc.com/data/UploadFile.aspx" method="post" enctype="multipart/form-data" >
<input type="file" name="F" style="width:160px;" />
<input type="submit" name="Submit" value="提交" />
</form>
(瀏覽器本身就是個APP,用戶點擊提交按鈕后,瀏覽器會以Post方式調用文件上傳接口;
給APP提供文件上傳接口和這類似,APP那邊的文件上傳程序只需要設置好相應的Http請求參數,比如上面的name="F",請求地址和請求方式等,就可以將文件傳到服務器)
后台代碼:
HttpPostedFile file = Request.Files["F"];
string filename = System.IO.Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath(filename));
如果是上傳圖片文件,前台程序可以先將圖片轉成Base64字符串,后端接口程序再把接收到的Base64字符串轉成圖片,比如:
string f = HttpContext.Current.Request["F"]; //與上面的方式不同,此時的F對於前台程序來說是普通的參數
MemoryStream ms = new MemoryStream(Convert.FromBase64String(f));
Bitmap bmp = new Bitmap(ms);
bmp.Save(HttpContext.Current.Server.MapPath("f.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
