【问题标题】:File is not uploading in ASP.Net MVC文件未在 ASP.Net MVC 中上传
【发布时间】:2016-08-07 15:17:38
【问题描述】:

大家好,我在上传文件并将其保存到文件夹时遇到问题,这是我的看法

<div class="admin-empty-dashboard">
@using (Html.BeginForm("UploadResumes", "Employee", new { enctype = "multipart/form-data" }))
{

    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div class="icon">
        <i class="ion-ios-book-outline"></i>
    </div>

    <h4>Welcome @Model.FullName!</h4>
    <p>@ViewBag.Error</p>
    <div class="form-group bootstrap-fileinput-style-01">
        <label>Upload Resume</label>
        <input type="file" name="file" required="required" class="btn btn-primary" style="margin-left:265px" accept="application/msword,text/plain, application/pdf">
        <span class="font12 font-italic">** File must not bigger than 2MB</span>
    </div>

    <input type="submit" name="name" class="btn btn-primary" value="UploadResumes" />
}

</div>

这是我的控制器操作问题,它的 HttpPostedFileBase 对象值始终为空,我运行 Request.Files.Count 它也给了我零,哪里有问题?

public ActionResult UploadResumes(HttpPostedFileBase file)
{
    if (Session["UserID"] != null && Session["UserName"] != null && Session["EmployeeID"] != null)
    {
        int id = Convert.ToInt32(Session["EmployeeID"]);
        string[] allowedExtenssions = new string[] { ".pdf", ".doc", ".docx" };
        string cvPath = "~/cvs/Employee_cvs/";
        if (!Directory.Exists(Server.MapPath(cvPath)))
            Directory.CreateDirectory(Server.MapPath(cvPath));

        MyDbContext db=new MyDbContext();
        tblEmployee employee = (from s in db.tblEmployees where s.UserId == id select s).FirstOrDefault();

        // HttpPostedFileBase cv = Request.Files["CV"];
        if (file != null)
        {
            if (file.ContentLength > 0)
            {

                string ext = Path.GetExtension(file.FileName);
                if (allowedExtenssions.Contains(ext.ToLower()))
                {
                    string fileName = DateTime.Now.Ticks + ext;
                    string filePath = cvPath + fileName;
                    string serverPath = Server.MapPath(filePath);
                    file.SaveAs(serverPath);
                    employee.cvUrl = filePath;
                }

            }
            ViewBag.Error = "Some Error Occured";
        }

        return RedirectToAction("Dashboard");

    }
    else
    {
        return RedirectToAction("Login", "User");
    }
}

【问题讨论】:

标签: c# asp.net asp.net-mvc


【解决方案1】:

如下所示更新并检查

@using (Html.BeginForm("UploadResumes", "Employee",FormMethod.Post, new { enctype = "multipart/form-data" }))
{

}

还有

[HttpPost]
public ActionResult UploadResumes(HttpPostedFileBase file)
{
}

【讨论】:

    【解决方案2】:

    尝试将控制器上的参数更改为

    public ActionResult UploadResumes(Model yourModel, HttpHostedFileBase file)
    

    【讨论】:

      【解决方案3】:

      在我的大多数需要上传文件的控制器中,通常是基于数组的。让你的函数参数像这样的数组:

      public ActionResult UploadResumes(HttpPostedFileBase[] files){
      }
      

      【讨论】:

        猜你喜欢
        • 2023-04-03
        • 1970-01-01
        • 2014-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-26
        • 2012-12-18
        相关资源
        最近更新 更多