xhr: function() { // create an XMLHttpRequest var xhr = new XMLHttpRequest();
// listen to the 'progress' event xhr.upload.addEventListener('progress', function(evt) {
if (evt.lengthComputable) { // calculate the percentage of upload completed var percentComplete = evt.loaded / evt.total; percentComplete = parseInt(percentComplete * 100);
// update the Bootstrap progress bar with the new percentage $('.progress-bar').text(percentComplete + '%'); $('.progress-bar').width(percentComplete + '%');
// once the upload reaches 100%, set the progress bar text to done if (percentComplete === 100) { $('.progress-bar').html('Done'); }
$('.upload-btn').on('click', function (){ $('#upload-input').click(); $('.progress-bar').text('0%'); $('.progress-bar').width('0%'); });
$('#upload-input').on('change', function(){
var files = $(this).get(0).files;
if (files.length > 0){ // create a FormData object which will be sent as the data payload in the // AJAX request var formData = new FormData();
// loop through all the selected files and add them to the formData object for (var i = 0; i < files.length; i++) { var file = files[i];
// add the files to formData object for the data payload formData.append('uploads[]', file, file.name); } functiongetCsrfToken() { var metas = document.getElementsByTagName('input'); for (i=0; i<metas.length; i++) { if (metas[i].getAttribute("name") == "_csrf") { console.log(metas[i].getAttribute("name")) return metas[i].getAttribute("value"); } } return""; } var csrf = getCsrfToken(); // console.log(csrf) $.ajax({ url: '/account/upload', type: 'POST', data: formData , headers: { '_csrf':csrf, 'X-CSRF-Token': csrf }, processData: false, contentType: false, success: function(data){ console.log('upload successful!\n' + data); }, error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response //alert(xhr.status); //console.log(xhr.responseText); }, xhr: function() { // create an XMLHttpRequest var xhr = new XMLHttpRequest();
// listen to the 'progress' event xhr.withCredentials = true; xhr.upload.addEventListener('progress', function(evt) {
if (evt.lengthComputable) { // calculate the percentage of upload completed var percentComplete = evt.loaded / evt.total; percentComplete = parseInt(percentComplete * 100);
// update the Bootstrap progress bar with the new percentage $('.progress-bar').text(percentComplete + '%'); $('.progress-bar').width(percentComplete + '%');
// once the upload reaches 100%, set the progress bar text to done if (percentComplete === 100) { $('.progress-bar').html('Done'); alert('upload successful!\n'); }
// create an incoming form object var form = new formidable.IncomingForm();
// specify that we want to allow the user to upload multiple files in a single request form.multiples = true;
// store all uploads in the /uploads directory form.uploadDir = path.join(__dirname, '/uploads');
// every time a file has been uploaded successfully, // rename it to it's orignal name form.on('file', function(field, file) { fs.rename(file.path, path.join(form.uploadDir, file.name)); });
// log any errors that occur form.on('error', function(err) { console.log('An error has occured: \n' + err); });
// once all the files have been uploaded, send a response to the client form.on('end', function() { res.cookie('fileName',fileName) res.end('/uploads/News/'+fileName); });
// parse the incoming request containing the form data form.parse(req);
});
var server = app.listen(3000, function(){ console.log('Server listening on port 3000'); });