0

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

6
  • are you talking about ajax?? you cant pass any client side value to server without it ... m not a .net expert but this is genenral Commented Jul 12, 2012 at 18:06
  • I have a dialog <input type="file" /> and want to get the file name and file path and pass it to my controller ... I can pass a value to my controller as such: post.push(name: 'name', value: 4); Commented Jul 12, 2012 at 18:08
  • in that case their is nothing to do with jquery ...u can pass it through form post or via url (dont know how you achieve this in .net), . If you want you can use ajax(via jquery/javascript) to post your values to server. Commented Jul 12, 2012 at 18:12
  • You actually probably won't be able to consistently get the file path, using a <input type="file" /> - for security reasons, a number of browsers display a fake path (C:\fakepath\filename in chrome, for instance) Commented Jul 12, 2012 at 18:18
  • so what will be your suggestion Chris? I need to get the file and file name and store them in a database .. the issue is how to pass them from view to my model. Commented Jul 12, 2012 at 18:19

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.