The jQuery file upload method is a very simple and yet easy method for uploading the files over HTTP. jQuery provides you with built in functions and objects that can be used to encode and transmit the files to the server... using ajax!
First of all, you are required to have a
FormData
object, which holds the data of the form and thus the file. You can create so using,
var formData = new FormData($('#form')[0]);
That is not enough. You are required to tune a few other things up before launching the request.
type: 'POST',
processData: false,
contentType: false,
data: formData,
The type of HTTP should be POST, after that you should set these other two properties to false. Data object is the data that would be passed along with the request. This would hold the FormData object. Now, when the request would be made (to current page if
url
is not provided) the file would be sent along with the request. You can access the files in a way that you would in your own server-side language. In ASP.NET you would write the following code,
files = Request.Files.Count;
if(files > 0) {
for (int i = 0; i < files; i++) {
var file = Request.Files[i];
string fileName = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("~/" + fileName));
}
}
For more please read this tip of mine:
Uploading the files – HTML5 and jQuery way![
^]