1

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);
}
Ehsan Sajjad
  • 59,154
  • 14
  • 90
  • 146
Nabia Saroosh
  • 169
  • 3
  • 13

1 Answers1

2

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!

Ehsan Sajjad
  • 59,154
  • 14
  • 90
  • 146
  • 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`. – jAC Jul 06 '17 at 09:55
  • in posted `file` object in above case it will only contain the filename with extension – Ehsan Sajjad Jul 06 '17 at 09:56
  • 1
    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: http://imgur.com/a/5gmPO We had this question here a few days ago with the `IFormFile` class: https://stackoverflow.com/questions/44718080/asp-net-core-file-upload-issue/44719038#44719038 – jAC Jul 06 '17 at 10:15
  • put a break point and see if `file.SaveAs` executes without any exception – Ehsan Sajjad Jul 06 '17 at 11:11
  • @EhsanSajjad Sure it saves without an exception, but it saves in the wrong directory/overwrites the uploaded file with the same content. – jAC Jul 06 '17 at 11:57