Asp.Net MVC3 上传文件问题
在MVC3中,我用HttpPostedFileBase作为上传文件的参数类型,但是在SavaAs方法中却提示不支持给定路径的格式,求解决方法所写代码如下...
在MVC3中,我用HttpPostedFileBase作为上传文件的参数类型,但是在SavaAs方法中却提示不支持给定路径的格式,求解决方法
所写代码如下 展开
所写代码如下 展开
展开全部
可以考虑一下以下代码:首先创建:一 创建表单
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })){ <input type="file" name="file" /> <input type="submit" value="OK" />}
二 创建controlle
public class HomeController : Controller{ // This action renders the form public ActionResult Index() { return View(); } // This action handles the form POST and the upload [HttpPost] public ActionResult Index(HttpPostedFileBase file) { // Verify that the user selected a file if (file != null && file.ContentLength > 0) { // extract only the fielname var fileName = Path.GetFileName(file.FileName); // store the file inside ~/App_Data/uploads folder var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path); } // redirect back to the index action to show the form once again return RedirectToAction("Index"); }}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })){ <input type="file" name="file" /> <input type="submit" value="OK" />}
二 创建controlle
public class HomeController : Controller{ // This action renders the form public ActionResult Index() { return View(); } // This action handles the form POST and the upload [HttpPost] public ActionResult Index(HttpPostedFileBase file) { // Verify that the user selected a file if (file != null && file.ContentLength > 0) { // extract only the fielname var fileName = Path.GetFileName(file.FileName); // store the file inside ~/App_Data/uploads folder var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path); } // redirect back to the index action to show the form once again return RedirectToAction("Index"); }}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询