5

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?

SteveC
  • 15,808
  • 23
  • 102
  • 173
Traffy
  • 2,803
  • 15
  • 52
  • 78

2 Answers2

4

As I understand, you need to upload the file to the server before opening it e.g.

 if (file != null && file.ContentLength > 0) 
    {
        // extract only the fielname
        var fileName = Path.GetFileName(file.FileName);
        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        file.SaveAs(path);
    }

I took the above from a similar question's answer by Chance and Darin Dimitrov : File Upload ASP.NET MVC 3.0

This answer additionally references the usefull blog post Uploading a File (Or Files) With ASP.NET MVC

Community
  • 1
  • 1
shawad
  • 355
  • 2
  • 6
  • Thanks for your answer ! I'll try it now and let you know. – Traffy Jul 26 '13 at 09:07
  • 1
    When the program arrives to the SaveAs method, I get this error : **Could not find a part of the path 'C:\Users\maab\Desktop\2013-05-10_BuSI Material Project - Last\BuSIMaterial\BuSIMaterial\App_Data\uploads\List_2013.xlsx'.** – Traffy Jul 26 '13 at 09:11
  • If this error is happening when you run it locally - have you created the uploads folder within App_Data? If it is happening when you publish, you may need to tweak the publish settings (right click on your project->Properties withing the Solution Explorer) and check that under Package/Publish that it deployes All files in this project folder). On top of this you may need to provide rights to the upload folder in IIS. – shawad Jul 26 '13 at 10:02
  • 1
    traffy im facing the same issue with htttpostedfile path did you found any solution? – rohit singh May 26 '15 at 06:07
2

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.

SteveC
  • 15,808
  • 23
  • 102
  • 173
sky
  • 441
  • 5
  • 17