Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the uploading file name before upload with jQuery File Upload plugin

I'm using the jQuery File Upload plugin https://github.com/blueimp/jQuery-File-Upload/wiki to upload multiple files to the server. I'll be using it with a custom web service which will store the uploaded images on the server. However, besides of the POST request body I'll also need to pass to the web service the name of the file which is being uploaded. So, with each file transfer I'll also need to pass to the web service the name of the file. Could anybody tell me how can I take the file name and send it with each upload request? I'll basically need to get it from the field, without any additional input field. If a user chose 10 files, I'll need to take the name for each file in the queue and submit it with the upload request.

Thanks.

like image 756
cycero Avatar asked Apr 24 '14 10:04

cycero


2 Answers

Ok, here's the solution which works:

// Storing the file name in the queue
var fileName = "";

// On file add assigning the name of that file to the variable to pass to the web service
$('#fileupload').bind('fileuploadadd', function (e, data) {
  $.each(data.files, function (index, file) {
    fileName = file.name;
  });
});

// On file upload submit - assigning the file name value to the form data
$('#fileupload').bind('fileuploadsubmit', function (e, data) {
  data.formData = {"file" : fileName};
});
like image 160
cycero Avatar answered Sep 21 '22 09:09

cycero


var filename = $('input[type=file]').val().split('\\').pop();
like image 45
manish nautiyal Avatar answered Sep 20 '22 09:09

manish nautiyal