0

In MVC looking for a way to select a file from a browse/file selector, and then hit submit.

But when I hit submit I don't want to upload the actual file, just want to store/retrieve the filepath selected.

Looking at examples like this, it seems like the file gets uploaded and stored into memory which is not what I want.

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
tereško
  • 58,060
  • 25
  • 98
  • 150
StevieB
  • 6,263
  • 38
  • 108
  • 193

2 Answers2

0

On the client capture the name of the file that the user selected and place it in a hidden file. When the user clicks submit, only submit the file name to an action method that takes string as input:

[HttpPost]
public ActionResult Index(string filename)
{
    //Do something
}

Since you did not provide code on how you are selecting the file, I can only suggest that you use a plugin that allows you to hook in your own javascript for the selection event (I use KendoUI Upload).

JTMon
  • 3,189
  • 22
  • 24
0

You can use Path.GetFileName method to get the file name with extension. GetFileName method accepts the full path to the file, which you can obtain from the FileName property of the posted file.

If you do not want to save it to sever disk, Don't do that. Just read the file name and do what you want to do with that.

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
  if (file != null)
  {
     string justFileName=Path.GetFileName(file.FileName);
     // No need to save the file. Just forget about it. You got your file name
     //do somethign with this now
  }
  //  TO DO : Return something
}

You need to import System.IO namespace to your class to use the Path class.

Shyju
  • 214,206
  • 104
  • 411
  • 497