0

I am beginner in asp.net and trying to upload the image in my project Images folder but it is not uploading it in desired folder. Anybody please give me suggestion.

Create.cshtml

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

    <div class="form-horizontal">
        <h4>lens</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })


        <div class="form-group">
            @Html.LabelFor(model => model.lens_img, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" name="file" id="file" style="width: 100%;" />
            </div>
        </div>
        <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>

    </div>
}

Controller.cs

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "lens_img")] lens lens, HttpPostedFileBase file)
{
    if (ModelState.IsValid)
    {
        if (file != null)
        {
            file.SaveAs(HttpContext.Server.MapPath("~/Content/Images/")
                                                          + file.FileName);
            lens.lens_img = file.FileName;
        }
        db.lenses.Add(lens);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(lens);
}
2
  • I can't see any Submit button in your Razor code. Commented Jul 6, 2017 at 9:44
  • Please see my edited question. Commented Jul 6, 2017 at 9:46

1 Answer 1

1

If file is reaching the controller action and file parameter is not null, then you should be using Path.Combine method to generate the correct path, do not use string concatenation for this, you should try it in following way:

file.SaveAs(Path.Combine(HttpContext.Server.MapPath("~/Content/Images/"), file.FileName);

For more clarity, let's break in to two steps:

var mappedPath = HttpContext.Server.MapPath("~/Content/Images/");
file.SaveAs(Path.Combine(mappedPath, file.FileName);

Also have a look at this answer as well which is related.

Hope it helps!

Sign up to request clarification or add additional context in comments.

5 Comments

Be aware that Path.Combine will fail if file.FileName is a path itself, e.g. C:\Users\USER\Desktop\myFile.jpg. So I'd wrap a Path.GetFileName() around file.FileName.
in posted file object in above case it will only contain the filename with extension
I thought so, too. But one day we ran an application in the Intranet as you do quite often and accessed it with IE. Now Internet Explorer has a special zone for that, in which it passes on the whole file path instead of the name. I just tested the code and the result was the whole file path, see here: imgur.com/a/5gmPO We had this question here a few days ago with the IFormFile class: stackoverflow.com/questions/44718080/…
put a break point and see if file.SaveAs executes without any exception
@EhsanSajjad Sure it saves without an exception, but it saves in the wrong directory/overwrites the uploaded file with the same content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.