File Upload in ASP.NET MVC 5

限于喜欢 提交于 2021-02-11 05:57:20

问题


I am unable to upload file in folder. I am not able to find the mistake. The UploadFile View returns on same view after uploading file.

Model Class:

public class Upload
    {
        public int UploadId { get; set; }
        public string UploadTitle { get; set; }
        public string UploadURL { get; set; }

    }

Here is the Controller(FileUpload) Action:

public ActionResult UploadFile(HttpPostedFileBase file, Upload upload)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string fil = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
                    file.SaveAs(path);
                    upload.UploadURL = "/Content/Uploads/Files/" + file.FileName;
                }
                db.Uploads.Add(upload);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(upload);
        }

In my View:

@using (Html.BeginForm("UploadFile, "FileUpload", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            <div class="control-label col-md-2">
                <label for="file">Upload Image  for Slide:</label>
            </div>
            <div class="col-md-10">
                <input type="file" name="file" id="file" style="width:50%" />
            </div>

        </div>



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

回答1:


Hi I have tried your same code its works for me.

Controller

   [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string fil = System.IO.Path.GetFileName(file.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
                    file.SaveAs(path);
                }
                return RedirectToAction("Index");
            }

            return View("UploadFile");
        }

View

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        <div class="control-label col-md-2">
            <label for="file">Upload Image  for Slide:</label>
        </div>
        <div class="col-md-10">
            <input type="file" name="file" id="file" style="width:50%" />
        </div>

    </div>



    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

I have found small mistake in you code in Html.BeginForm in action name " (double quotes is missing)




回答2:


I forgot to mention the required field on UploadURL in above model class:

public class Upload
    {
        public int UploadId { get; set; }
        public string UploadTitle { get; set; }

        [Required]
        public string UploadURL { get; set; }

    }

Required Field validation on UploadURL field restricted the file upload here. I removed the Required field validation from the field.



来源:https://stackoverflow.com/questions/30457784/file-upload-in-asp-net-mvc-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!