esastincy
esastincy

Reputation: 1627

Getting file path for uploaded file in MVC 3

I am having a hard time believing that there is no way to get the full path of a file being uploaded to a server in MVC3 (I have read that this is for security purposes).

Is there a work around? I am writing a small app where the users will upload a file, edit it on the screen, and then save it back to the original location. Does anyone have a good way of doing this? Do I have to use something other than HttpPostedFileBase?

Upvotes: 1

Views: 4402

Answers (3)

Prince
Prince

Reputation: 328

To get File Name and path you can do this::

  foreach (string filesData in Request.Files)
  {
       var fileNm = Request.Files[filesData].FileName;
       HttpPostedFileBase hpf = Request.Files[filesData] as HttpPostedFileBase;
       string filePath = Path.Combine(HttpContext.Server.MapPath("~//Your Folder Path//"));
       hpf.SaveAs(filePath + fileNm);
 }

Hope This Will help You..

Upvotes: 0

Jitender Kumar
Jitender Kumar

Reputation: 2587

Not possible, There is no way to play with file on client side. First you need to upload the file on server, perform action on it from there and save again in client machine.

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

    }

and in controller you can have your action method something like this:

    [HttpPost]
    public ActionResult EditImage(AddNewProductModel model, HttpPostedFileBase file)
    {

    }

Hope this solution will help you.

Upvotes: 0

db2
db2

Reputation: 517

Not possible. The web server can only know as much about the file as the user's web browser is willing to tell it, and any current browser will only give the original filename in addition to the file content. The file's path on the client PC is never sent to the server. It also seems to be impossible to get this information with client-side Javascript, as a security precaution.

In addition, the web server would have no control over where on the user's PC the (re)downloaded file would be saved, so having this path information would be of little use anyway.

Upvotes: 5

Related Questions