Doctor
Doctor

Reputation: 57

Jquery passing the name of file and file path from input dialog.. to controller MVC 3

How can I pass an input dialog file name and file path using jquery / json to controller using MVC 3?

I am using ado.net.

any help or anywhere to look for this?

Thanks

Upvotes: 0

Views: 1359

Answers (1)

Chris Carew
Chris Carew

Reputation: 1418

Going to post an answer on how to upload a file in MVC3

In your controller, you need a method

[HttpPost]
public ActionResult FileUpload( HttpPostedFileBase file )
{
    byte[] buffer = new byte[file.InputStream.Length];
    file.InputStream.Read( buffer, 0, (int)file.InputStream.Length );
    // You need to have some method of saving the bytes of this file, or use the 
    // file.SaveAs() method to save this to a targeted directory your web server
    // has access to
    YourCode.SaveFile( buffer, file.FileName );
    return View( );
}

And a view set up like so:

@using ( Html.BeginForm("FileUpload","YourControllerName",FormMethod.Post,new { enctype="multipart/form-data"}))
{
    <input type="file" id="file" name="file" />
    <input type="submit" />
}

This will upload the file

Upvotes: 1

Related Questions