0

I simply tried this, but its not working, what is the problem in it,

MY index page:

 @{
 ViewBag.Title = "Index";
 }

  @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/from-    data" }))
    { 
       <div>
       <h1 style="align-content: center; color: blueviolet">Application to upload files</h1>
       </div>
       <div>
       <input type="file" id="file" name="file" />
       <br />
       <input type="submit" id="load" name="submit" value="Submit" />
       </div>

       }

And My controller is,

                  [HttpPost]
    public ActionResult Upload()
    {
        string path = @"~/Content/Upload";

        HttpPostedFileBase file = Request.Files["file"];

        if (file != null)
            file.SaveAs(path + file.FileName);

        return Content("Sucess");
    }
6
  • "It's not working". What isn't working? What error are you getting? What exactly are you trying to accomplish with your code? Commented Sep 13, 2013 at 12:21
  • 2
    "mulitipart/from- data" doesn't look quite right. Try "multipart/form-data" Commented Sep 13, 2013 at 12:22
  • i tried "multipart/from-data" isn't worked, actually the error is Object reference not set to an instance of an object, when i upload the file, and pass the value to controller, file value is null in controller. Commented Sep 13, 2013 at 12:28
  • 2
    form, not from Commented Sep 13, 2013 at 12:39
  • @Mahendra Please pay attention to the detail of the enctype value that commenters are trying to point out, and it would also be helpful if you could include information about what exactly is not working. Commented Sep 13, 2013 at 12:43

1 Answer 1

1

The path you are attempting to save your file to looks wrong. Try with MapPath:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
    string path = Server.MapPath("~/Content/Upload");
    if (file != null)
    {
        file.SaveAs(Path.Combine(path, file.FileName));
    }

    return Content("Sucess");
}

Also make sure that you have used the correct enctype attribute in your form:

enctype = "multipart/form-data"

instead of:

enctype = "multipart/from-    data"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.