Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery fileupload - get list of uploaded files

I'm using the very good jquery plugin blueimp / jQuery-File-Upload

$('#fileupload').fileupload({
  autoUpload: true
, filesContainer: '#attachments_presentation tbody'
, stop: function (e) {
    console.log(data);
  }
});

And I'm not able to do something very simple: get the list of uploaded files
(with their name on the server)

Firstly I naively though it would be stored on the file input so I could get the list with

$('#fileupload').val();

But it's not, so I though about something like

$('#fileupload').fileupload('getfiles');

I can't find the way from the docs

Can somebody tell me how I can get the list of uploaded file names in the server ?


Update

I'd like to get the uploaded files name on the server in order to be able to manage them afterwards.

For example:

  • I upload a file named trutruc.pdf
  • I upload test.png
  • I re-upload a file named trutruc.pdf
  • I delete the first file

The current list of files in the server should then be test.png, trutruc (2).pdf


Update 2

I'm using the default php script shipped with fileUpload

like image 910
Pierre de LESPINAY Avatar asked Mar 16 '13 11:03

Pierre de LESPINAY


People also ask

What is the use of jQuery File Upload plugin?

jQuery File UPload plugin provides Multiple file uploads with progress bar. jQuery File Upload Plugin depends on Ajax Form Plugin, So Github contains source code with and without Form plugin. 1). Add the CSS and JS files in the head sections

What is the best file upload library for JavaScript?

FileDrop.js is a JavaScript optimized file uploading library that focuses on direct browser file uploading. It has custom notification alerts, without much other functionality. Select or drag your files, click upload and wait for you to be notified of a successful upload. There’s the ability to enable multiple file uploads if need be.

How to get list of uploaded file names from the server?

To get the list of uploaded file names from the server, requires server-side code: UploadHandler.php is used to upload files to a directory/s on your server. If you've downloaded the plug-in archive and extracted it to your server, the files will be stored in php/files by default.

What is the best file uploader for form upload?

Works with any server-side platform (PHP, Python, Ruby on Rails, Java, Node.js, Go etc.) that supports standard HTML form file uploads. jQuery Ajax File Uploader is a great example of how simple (yet modern) file uploaders can be.


4 Answers

I use UI version. Here is script that grabs server links from table of files and populates array:

    var files = new Array();
    $("#fileupload td p.name a").each(function() {
        var name = $(this).attr("href");
        files.push(name);
    });
    alert(files.join('\n'));
like image 134
Eugene Mala Avatar answered Oct 10 '22 00:10

Eugene Mala


you can also try this

 $('#fileupload').bind('fileuploadcompleted', function (e, data) {
     var filess= data.result.files[0];
        var filenam = filess.name;
       alert(filenam);
});
like image 28
Harpreet Kaur Avatar answered Oct 09 '22 22:10

Harpreet Kaur


I had the same problem and after a few hours and tens of file uploads, I think I have the answer you need:

done: function (e, data) {
    response = data.response();
    parsedresponse = $.parseJSON(response.result);
    console.log(parsedresponse.files);
}

As you can see, you will find the uploaded files name at parsedresponse.files[i].url (i is the 0-based index of the uploaded files).

Ok... its the uploaded file's URL, but you'll be able to extract the final file name ...

Hope it helps. (this is nowhere in the documentation, I managed to get to this solution by checking the Firebug console outputs)

like image 44
Alexandre Paulo Avatar answered Oct 09 '22 23:10

Alexandre Paulo


When you instantiate the default plugin, there is a code portion which is retrieving all the previous uploaded files (assuming you haven't changed the server code) :

See on the main.js file line 70 :

    // Load existing files:
    $.ajax({
    // Uncomment the following to send cross-domain cookies:
    //xhrFields: {withCredentials: true},
    url: $('#fileupload').fileupload('option', 'url'),
        dataType: 'json',
        context: $('#fileupload')[0]
    }).done(function (result) {
        $(this).fileupload('option', 'done')
            .call(this, null, {result: result});
    });

Than if you look further in your code, there is a table which will be filled out with the template :

<table role="presentation" class="table table-striped"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody></table>

You could easily parse each from that table after loading the files :

$('tbody.files tr').each(function(i, e) {
    //according to @Brandon's comment it's now p.name
    var name = $(e).find('td.name').text(); 
    var size = $(e).find('td.size').text();
    //etc.
});
like image 31
soyuka Avatar answered Oct 10 '22 00:10

soyuka