i just have input type file and want to move csv file without any third party js plugin ?
<input type="file" name="file" id="file" />
<input type="button" name="upload" id="upload" value="Upload" />
i just have input type file and want to move csv file without any third party js plugin ?
<input type="file" name="file" id="file" />
<input type="button" name="upload" id="upload" value="Upload" />
You can use the FormData interface. Note that this doesn't work on IE lower than 10.
Here's a great tutorial on how to use it: http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax
In short, you have to bind a change event on the file input to capture the selected file and on the form submit you'd create a new FormData object to which you'd append the file data previously saved.
// Variable to store your files
var files;
// Add events
$('input[type=file]').on('change', prepareUpload);
// Grab the files and set them to our variable
function prepareUpload(event)
{
files = event.target.files;
}
On form submit:
var data = new FormData();
$.each(files, function(key, value)
{
data.append(key, value);
});
$.ajax({
url: 'submit.php?files',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR)
{
if(typeof data.error === 'undefined')
{
// Success so call function to process the form
submitForm(event, data);
}
else
{
// Handle errors here
console.log('ERRORS: ' + data.error);
}
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
console.log('ERRORS: ' + textStatus);
// STOP LOADING SPINNER
}
});