File upload MVC5文件上传
有人能帮帮我吗。我一直在学习如何使用MVC5使用fileupload的教程,但是文件一直无法上传 我的App_数据文件夹中有一个Uploads文件夹,文件应该保存到该文件夹中。在我的控制器中,我有:File upload MVC5文件上传,file-upload,asp.net-mvc-5,visual-studio-2017,File Upload,Asp.net Mvc 5,Visual Studio 2017,有人能帮帮我吗。我一直在学习如何使用MVC5使用fileupload的教程,但是文件一直无法上传 我的App_数据文件夹中有一个Uploads文件夹,文件应该保存到该文件夹中。在我的控制器中,我有: using System.IO; namespace [project_name].Controllers public class [controller_name]: Controller { [HttpGet] public ActionResult I
using System.IO;
namespace [project_name].Controllers
public class [controller_name]: Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
ViewBag.Message = "";
try
{
// Verify that the user selected a file
if (file.ContentLength > 0)
{
// extract only the filename
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
ViewBag.Message = "File Upload Successful";
return RedirectToAction("Index");
}
catch(Exception ex)
{
ViewBag.Message = ex.Message;
return View();
}
}
}
@{
ViewBag.Title = "File Upload";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-4">
@using (Html.BeginForm("Index", "[controller_name]", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@ViewBag.Message<br />
@Html.TextBox("file", "", new { type = "file" })
<input type="submit" value="Upload" />
}
</div>
</div>
<br />
我认为:
using System.IO;
namespace [project_name].Controllers
public class [controller_name]: Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
ViewBag.Message = "";
try
{
// Verify that the user selected a file
if (file.ContentLength > 0)
{
// extract only the filename
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
ViewBag.Message = "File Upload Successful";
return RedirectToAction("Index");
}
catch(Exception ex)
{
ViewBag.Message = ex.Message;
return View();
}
}
}
@{
ViewBag.Title = "File Upload";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-4">
@using (Html.BeginForm("Index", "[controller_name]", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@ViewBag.Message<br />
@Html.TextBox("file", "", new { type = "file" })
<input type="submit" value="Upload" />
}
</div>
</div>
<br />
我添加了视图,但是当我再次尝试时,我遇到了相同的问题。您尝试这样做应该相当简单,并且我认为您已经获得了所需的所有信息。如果不是这样,很抱歉:( 如果你的问题没有解决,我鼓励你开始一个新的“hello world”项目,“从头开始”,如下所示:
- 来源文章:
public const string UPLOADS_FOLDER = @"c:\temp";
public ActionResult Index() { ... }
[HttpGet] public ActionResult UploadFile() { ... }
[HttpPost] public ActionResult UploadFile(HttpPostedFileBase file) { ... }
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace SimpleUpload.Controllers
{
public class UploadController : Controller
{
public const string UPLOADS_FOLDER = @"c:\temp";
// GET: Upload
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult UploadFile()
{
return View();
}
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
try
{
if (file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
string path = Path.Combine(UPLOADS_FOLDER, fileName);
file.SaveAs(path);
}
ViewBag.Message = "File Uploaded Successfully!!";
return View();
}
catch
{
ViewBag.Message = "File upload failed!!";
return View();
}
}
}
}
Views\Upload\UploadFile.cshtml
@{
ViewBag.Title = "UploadFile";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.TextBox("file", "", new { type = "file" }) <br />
<input type="submit" value="Upload" />
@ViewBag.Message
</div>
}
@{
ViewBag.Title=“上传文件”;
}
@视图包。标题
@使用(Html.BeginForm(“UploadFile”,“Upload”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
@Html.TextBox(“文件“,”,new{type=“file”})
@查看包。留言
}
我假设您正在本地文件系统上选择一个有效文件,该文件未被使用,并且您具有读取权限。听起来您还成功调用了POST ActionHandler。请将POST ActionHandler重命名为“Index”之外的其他名称。UploadFile(HttpPostedFileBase文件)
很好。如果这有什么不同,请告诉我们。另请参阅:不走运。同样的错误。问:你解决了这个问题吗?我猜是“不”,我不知道为什么。所以我冒昧地更新了我的帖子,用了一个完整的“从头开始”示例。我鼓励您也这样做,并将找到的内容发回。仍然不走运。现在我遇到以下服务器错误:找不到资源。说明:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下URL并确保其拼写正确。请求的URL:/HROFileUpload/索引版本信息:Microsoft.NET Framework版本:4.0.30319;ASP.NET版本:4.8.4210.0,URL为:“?”我将在今天晚些时候讨论这个问题,并让您知道它是如何进行的。我非常感谢您的耐心和愿意提供帮助!我创建了一个新项目,并按照您提供的链接中的说明进行操作,viola它工作得非常好。所以现在的问题是,为什么它不能与@using Html.BeginForm()一起工作在我的项目中。感谢您的帮助,我今天将深入研究这一点,看看我能想出什么。我将在找到有效的解决方案后发布更新。很高兴您能让它起作用!请记住,从我们之前的讨论中,按照惯例,“Index”这个名称是“special”。具体地说,控制器中的Index()
方法将由HTTP“GET”调用请求http://myurl/
。显式尾随“/”或隐式http://myurl
。这可能(也可能不)与您的故障排除有关。。。