在C#中有时做站点或者Web系统不得不用到html标签作为表单提交的对象,例如在MVC的Razor模型视图中,就会使用<input type="file" />传递上传文件,那么后台代码肯定需要做相应的处理,下面这段代码就会派上大用场:
1 HttpFileCollection files = Request.Files;
//这里只能用<input type="file" />才能有效果,因为服务器控件是HttpInputFile类型
2 HttpPostedFile postFile = files[
0];
3 Stream file = files[
0].InputStream;
// 两种任选其一
4 string strUploadPath = HttpContext.Current.Server.MapPath(
"~/Content/MoBan/Temp/" +
strFileName);
5 postFile.SaveAs(strUploadPath);
6
转载于:https://www.cnblogs.com/sunnypron/p/5422343.html