How to programatically add files to the Plupload queue?

2.1k Views Asked by At

Since my previous question wasn't answered, let me put it in a different way.
I would like someone to provide a functional example on how to programatically add a file to plupload queue, ready for uploading.
Perhaps something like this:

$('#uploader').on("ready", function() { 
    uploader.addFile("c:\\file.jpg");
});

This code isn't working, though.
Thanks.

EDIT: Following Elijah Lynn's answer, I edited my code example to load a file from the server, not from the client.

$('#uploader').on("ready", function() { 
    uploader.addFile("server_path/file.jpg");
});
3

There are 3 best solutions below

0
On

This is what worked for me:

const file = item.getAsFile();

$('#uploader').pluploadQueue().addFile(file, file.name)
// It might just be for you
uploader.addFile(file, file.name)

You can get more info here https://www.plupload.com/docs/v2/Uploader#addFile-method-filefileName

I do not think you can upload file from your local drive, but you can through a custom drag and drop like you can see here:

https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop

0
On

The only code that worked so far is:

$(document).ready(function(){
    plupload = $("#uploader").plupload('getUploader');
    var img = new mOxie.Image();
    var url = o.resolveUrl("server_path/file.jpg");
    img.onload = function() {
        plupload.addFile(img.getAsBlob());
    }
    img.load(url);
});

This auto-loads an image from the server, after the document is ready. The credit for the code above goes to an user called Cinza.

I could not implement Calen's code. Maybe he wants to give us a functional example.

2
On

I just attempted to do this and I don't think it is possible to do this. The reason being security. Imagine if you created a script like you suggest with a link to a picture and just loaded it on 100,000 clients. One of those clients might just have a link to that photo and off it would go to your website! And you wouldn't have needed their permission.

That being said, there is an addFile() method that Plupload provides but it needs a File object and getting that File object programatically is the forbidden part. The core issue is getting the File reference in order to then get the File object.