在plupload ui中成功上传后如何获取/打印文件名

时间:2019-03-06 05:48:06

标签: javascript php upload plupload

我对javascript不太了解。上载成功后,我试图在输入框中获取并打印文件名。我正在使用plupload ui小部件。以下脚本可以正常工作,我可以上传文件,但上传完成后无法获取文件名。我在Google上搜索了很多,但无法正常运行。 这是我正在使用的代码。

// Initialize the widget when the DOM is ready
$(function() {
    $("#uploader").plupload({
        // General settings
        runtimes : 'html5,flash,silverlight,html4',
        url : 'upload.php',

        // User can upload no more then 20 files in one go (sets multiple_queues to false)
        max_file_count: 20,

        chunk_size: '1mb',


        filters : {
            // Maximum file size
            max_file_size : '10mb',
            // Specify what files to browse for
            mime_types: [
            {title : "Image files", extensions : "jpg,gif,png"},
    {title : "Zip files", extensions : "zip"}
        ]
        },

        // 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,
        prevent_duplicates: true,

        // Views to activate
        views: {
            list: true,
            thumbs: true, // Show thumbs
            active: 'thumbs'
        },

        // Flash settings
        flash_swf_url : 'js/Moxie.swf',

        // Silverlight settings
        silverlight_xap_url : 'js/Moxie.xap'
        });

uploader.bind('FileUploaded' , function(up, file, response ) {
if ( (Uploader.total.uploaded + 1) == Uploader.files.length)
{
alert(File.name);
};
})


    // Handle the case when form was submitted before uploading has finished
    $('form').submit(function(e) {
var uploader = $('#uploader').plupload('getUploader');
        // Files in queue upload them first
        if (uploader.files.length > 0) {
        // When all files are uploaded submit form
            uploader.bind('StateChanged', function() {
if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                $('form')[0].getlink();
}
            });

            uploader.start();
        } else {
            alert("You must have at least one file in the queue.");
        }
        return false; // Keep the form from submitting
    });
});

1 个答案:

答案 0 :(得分:0)

您的FileUploaded处理程序中有一个错误的条件:

if ( (Uploader.total.uploaded + 1) == Uploader.files.length)

为什么Uploader.total.uploaded + 1?它永远不会实现。

Plupload UI小部件还具有用于将处理程序附加到事件的自己的语法。

// Subscribing to the events...
// ... on initialization:
$('#uploader').plupload({
  ...
  viewchanged: function(event, args) {
    // stuff ...
  }
});
// ... or after initialization
$('#uploader').on("viewchanged", function(event, args) {
  // stuff ...
});

请考虑以下playground sample