Search code examples
c#asp.net-mvcfile-uploadfilepathhttppostedfilebase

Getting the file path from HttpPostedFileBase


I'm working with ASP.NET MVC 4 and I'm trying to get the path of an uploaded file in order to open and manipulate it. This is how I proceed :

Controller

public ActionResult Bulk(HttpPostedFileBase file)
{
    FileStream fs = System.IO.File.Open(Server.MapPath(file.FileName), 
                                         FileMode.Open, FileAccess.Read);

    return RedirectToAction("Index");
}

View

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

    @using (Html.BeginForm("Bulk", "Bulk", null, FormMethod.Post, new 
                                          { enctype = "multipart/form-data" }))
    {
        @Html.ValidationSummary(true)

        <fieldset>
            <p>
                <input type="file" name="file"/>
            </p>
            <div class="form-actions">
              <button type="submit" class="btn btn-primary">Create</button>
            </div>
        </fieldset>
    }

When I'm doing that, I get an error which says ... Could not find a part of the path ...

How can I get the path where my file is actually located?


Solution

  • Can't comment as I don't have enough reputation ... :(

    Does the folder ...

    C:\Users\maab\Desktop\2013-05-10_BuSI Material Project -Last\BuSIMaterial\BuSIMaterial\App_Data\uploads

    exist? If not, try to create it manually and try your upload again.