以調用介面的方式將檔案上傳至Web伺服器

來源:互聯網
上載者:User

標籤:

 前台代碼:

<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);

以調用介面的方式將檔案上傳至Web伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.