How to return data via Ajax using Plupload at boot time?

I have been trying for the last few hours to get something ... something back from the pluploader after the completion of the queue to no avail.

Here is my JS code:

var uploader = $('#pluploadDiv').pluploadBootstrap(); uploader.bind("UploadComplete", function(up, files) { var obj = $.parseJSON(response.response); alert(obj.result); }); 

In the very last line of the upload.php script, I have:

 die('{"jsonrpc" : "2.0", "result" : "'.$_REQUEST['unitID'].'", "id" : "id"}'); 

This makes sense to me ... but it doesn’t work, the files load without problems, but the warning does not even work ... there is no answer at all.

Thoughts?

EDIT WITH NEW CODE AS SOLUTION

JS I use (thanks jbl):

 var uploader = $('#pluploadDiv').pluploadBootstrap(); uploader.bind('FileUploaded', function(upldr, file, object) { var myData; try { myData = eval(object.response); } catch(err) { myData = eval('(' + object.response + ')'); } $("#vehicle_id_value").val(myData.result); }); 

Upload.PHP script remains the same, the last line of code:

 die('{"jsonrpc" : "2.0", "result" : "'.$_REQUEST['unitID'].'", "id" : "id"}'); 

So basically, when I create a shell line for linking images in the upload script, I pass the line ID back to the original form in the hidden input field through the FileUploaded event associated with the plupload object.

 <input type="hidden" name="vehicle_id_value" id="vehicle_id_value" value="" /> 

It works like a charm!

+6
source share
4 answers

Several files could be downloaded as part of the download process. Individual responses are no longer available if at the UploadComplete stage. If you want to display information about downloading a specific file, you must bind to the FileUploaded event instead of UploadComplete . Sort of:

 uploader.bind('FileUploaded', function(upldr, file, object) { var myData; try { myData = eval(object.response); } catch(err) { myData = eval('(' + object.response + ')'); } alert(myData.result); }); 

Hope this helps

+11
source

Have you tried echo instead of death?

 echo '{"jsonrpc" : "2.0", "result" : "'.$_REQUEST['unitID'].'", "id" : "id"}'; 
+1
source

 function fileupload(fileuploadid, urlashx, foldername, keyid, filelimit, filefilters) { $("#" + fileuploadid).plupload({ // General settings runtimes: 'html5,flash,silverlight,html4', url: urlashx, //Set parameter for server side multipart_params: { foldername: foldername, keyid: keyid }, // Maximum file size max_file_size: filelimit, // User can upload no more then 20 files in one go (sets multiple_queues to false) max_file_count: 20, multiple_queues: true, //chunk_size: '10mb', // Resize images on clientside if we can resize: { //width: 200, //height: 200, quality: 90, crop: false // crop to exact dimensions }, // Specify what files to browse for filters: [ { title: "Allowed files", extensions: filefilters } ], // Rename files by clicking on their titles rename: true, // Sort files sortable: true, // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that) dragdrop: true, // Views to activate views: { list: true, thumbs: true, // Show thumbs active: 'thumbs' }, // Flash settings flash_swf_url: 'plupload/js/Moxie.swf', // Silverlight settings silverlight_xap_url: 'plupload/js/Moxie.xap', // Post init events, bound after the internal events init: { FileUploaded: function (up, file, jsonMsg) { var json = JSON.parse(jsonMsg.response); // now I have json object if (json.success) { AlertMessage("Message", json.message, "success", "False"); } else { AlertMessage("Message", json.message, "error", "False"); } up.splice(); //remove items of container up.refresh(); //refresh container } } }); } 

+1
source
 uploader.bind('FileUploaded', function (up, file, res) { var res1 = res.response.replace('"{', '{').replace('}"', '}'); var objResponse = JSON.parse(res1); alert(objResponse.fn); }); 
0
source

Source: https://habr.com/ru/post/945029/


All Articles