asp.net mvc下文件上传

时间:2014-09-05 14:11:51   收藏:0   阅读:195
典型的文件上传表单

<form action="/File" enctype="multipart/form-data"  

    method="post"/>

    <input type="file" name="file" />

    <input type="submit" name="upload" value="提交" />

</form>

文件上传操作

public ActionResult Index(HttpPostedFileBase file)

if (file != null)
{
               
     string strpath = Server.MapPath("~/files/"+ file.FileName);
     //获取文件路径
     if (file.FileName.LastIndexOf(@"\") > 0)
     {
          strpath = Server.MapPath("~/files/"+ file.FileName.Substring(file.FileName.LastIndexOf(@"\")));
     }
     file.SaveAs(strpath);//把图片保存在此路径中
     return Content("<script>alert(‘上传文件成功!‘);location.href=‘" + Url.Content("~/File") + "‘</script>");
}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!