mvc中文件上传下载

时间:2024-10-20 00:05:50

//控制器

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>