代码改变世界

mvc中文件上传下载

2019-03-13 11:23  .net小跟班(杜)  阅读(193)  评论(0编辑  收藏  举报

//控制器

public ActionResult FileUpLoad(HttpPostedFileBase f1)
{
string path = Server.MapPath("~/Picture");
string filename = Path.Combine(path,f1.FileName);
f1.SaveAs(filename);
return Content("OK");
}
public ActionResult FileDownLoad()
{
string path = Server.MapPath(@"C:\Users\Administrator\Desktop\自己练习\上传下载\UpLoad\UpLoad\Picture\QQ图片20190228155326.png");
FileStream fs = new FileStream(path,FileMode.Open);
return File(fs,"text/plain","a.jpg");
}

 

//视图

<body>
<div> 
<form action="/Demo/FileUpload" enctype="multipart/form-data" method="post">
<input id="f1" type="file" name="f1" />
<input id="Submit1" type="submit" value="上传" />
</form>
<a href="#/Demo/FileDownLoad">下载</a>
</div>
</body>