Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a file to a server using JSON and jQuery

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!

like image 986
Flavio CF Oliveira Avatar asked May 26 '11 16:05

Flavio CF Oliveira


People also ask

How do I POST JSON to the server?

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.

How JSON file is used in jQuery?

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.

Can JavaScript send data to 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.


1 Answers

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

like image 132
Barkermn01 Avatar answered Oct 16 '22 11:10

Barkermn01