I've to send a file to my server using jQuery. How can I do it using JSON?
The file type is not important, I intend to receive a byteArray or something like that on my server application.
If it helps I'm using an ASP.NET application, but my problem is not the way I handle that on server side, the issue is how to send the file data using jQuery.
I've found some upload plugins for jQuery that use a Flash file, others that do not use Flash, but what I really want to know is how the process works, not just use something already created by someone!
Send JSON Data from the Client SideUse JSON. stringify() to convert the JavaScript object into a JSON string. Send the URL-encoded JSON string to the server as part of the HTTP Request. This can be done using the HEAD, GET, or POST method by assigning the JSON string to a variable.
To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.
When sending data to a web server, the data has to be a string. So we are using JSON. stringify() function to convert data to string and send it via XHR request to the server.
jQuery is JavaScript. I think you mean to send the data via Ajax which can not be done. The best thing you can do is to use an iframe to a page that uploads the file to a temporary directory (not the default temp), and then provides the information back to the parent page as to where the file is saved then you use it.
There is no way to upload a file to a server using Ajax. It's too insecure.
I retract the Statement above NOW
You are now able to post/download file data using Javascript it's very complicated for a novice to understand but is now possible unlike when this question was answered
Using HTML5 file uploads with AJAX and jQuery
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With