pistacchio
pistacchio

Reputation: 58883

ASP.NET MVC: jQuery Post and files

I have a simple form with some text inputs and a file input. I do not send it directly with a post, but I read each value of the text inputs with jQuery, compose a $.post request and send it to the server.

How can I do the same with a file <input type = "file" />? I mean, sending it via ajax to a ASP.NET MVC page?

Thanks in advance

Upvotes: 2

Views: 631

Answers (2)

James S
James S

Reputation: 3374

You can't. That would require javascript being able to read the file from your hard drive and send it to the website... the browser security model wouldn't allow for it.

You can do some javascript DOM trickery where have the file element in an iframe and use javascript to submit the form -- this is what google does with gmail. There are also flash methods of doing this.

James

Upvotes: 3

redsquare
redsquare

Reputation: 78667

You cannot use an xhr request to upload a file.

You have two options. Use an iframe or use a flash uploader.

See this similar question & answers.

Upvotes: 4

Related Questions