以调用接口的方式将文件上传至Web服务器

 前台代码:

<form id="form1" action="http://www.abc.com/data/UploadFile.aspx" method="post" enctype="multipart/form-data" >
  <input type="file" name="F" style="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);

原文地址:https://www.cnblogs.com/Arlar/p/5934938.html