Getting file path from HttpPostedFileBase

I am working with ASP.NET MVC 4 and I am trying to get the path of the downloaded file to open and manipulate it. This is how I continue:

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 do this, I get an error message ... Could not find a part of the path ...

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

+4
source share
2 answers

I can not comment, because I do not have enough reputation ...: (

Is there a folder ...

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

exists? If not, try creating it manually and try downloading again.

+1
source

As I understand it, you need to upload the file to the server before opening it, for example.

  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 from the answer of Chance and Darin Dimitrov: Download ASP.NET MVC 3.0 file

This answer further refers to a useful blog post Uploading a file (or files) using ASP.NET MVC

+2
source

Source: https://habr.com/ru/post/1493617/


All Articles