简体   繁体   English

对于重新初始化丢失的正在进行的上传,plupload dynamic multipart_params

[英]plupload dynamic multipart_params for in progress uploads lost on re-initialization

I have a view in which I initialize a pluploader. 我有一个初始化pluploader的视图。 I am able to leave this view and still allow the uploads to complete. 我可以离开此视图,仍然允许上传完成。 The problem I am having is that whenever I re-enter the view, and there are uploads that are still in progress, the last multipart_param for "processId" that was assigned within "BeforeUpload" is used repeatedly instead of individually for each file. 我遇到的问题是,每当我重新进入视图时,仍然有正在进行的上载,将重复使用在“ BeforeUpload”中分配的“ processId”的最后一个multipart_param,而不是每个文件都单独使用。

Here's an example: I drop 5 files on the view. 这是一个示例:我在视图上删除了5个文件。 I leave the view. 我离开视图。 Meanwhile 2 files have completed upload progress. 同时,有2个文件已完成上传进度。 The processId for file 1 was "0123" and the processId for file 2 was "0124". 文件1的processId为“ 0123”,文件2的processId为“ 0124”。 I then re-enter the view. 然后,我重新进入视图。 This script is ran again. 该脚本再次运行。 The remaining files are all assigned processId 0124. 其余文件均分配有processId 0124。

I'm sure this is happening because the script is being ran any time you enter the view and something is being overwritten, but I've yet to find a solution. 我敢肯定这是因为您进入视图时都在运行脚本,并且某些内容已被覆盖,但是我还没有找到解决方案。

You can see within the uploader.fileAdded area, that I create a random series of int called a processId with this line 您可以在uploader.fileAdded区域中看到,我在此行中创建了一个随机的int序列,称为processId

var processId = process.createProcessId(file.index);

Then below, within BeforeUpload, I assign that processId to a multipart_param so that I can assign a unique processId to each upload. 然后在下面的BeforeUpload中,将那个processId分配给multipart_param,以便为每个上载分配一个唯一的processId。

uploader.settings.multipart_params.processId        = file.processId;

The Script 剧本

/* detects when a user drags files from the desktop */
initDragDetector:function(){
    var runtimes        = ui.getUploadRunTimes();
    var tmpId           = Math.random();                

    uploader = new plupload.Uploader({
        runtimes            :runtimes,
        max_file_size       :'500mb',
        url                 :jQuery('#upSrvUrl').val(),
        drop_element        :'uploadOverlay',
        browse_button       :'uploadOverlay',
        filters : [
            {title : "Image files", extensions : "jpg,tif,png,psd"},
        ],
        max_file_count      :100,
        multipart_params    :{rnd_id:Math.random(),tmp_sid:tmpId},
        multi_selection     :true,
        file_data_name      :'upfile_0',
    });

    //INITIALIZATION HANDLER 1
    uploader.bind('Init',function(up, params){
        uploader.settings.multipart_params.mount                = jQuery('#mount').val(); 
        uploader.settings.multipart_params.publisherId          = jQuery('#publisherId').val();
        uploader.settings.multipart_params.upload_range         = 100;

        if(uploader.features.dragdrop){
            var el      = jQuery("#uploadOverlay");

            el[0].ondragover = function(event) {
                event.dataTransfer.dropEffect = "copy";
            };
            el[0].ondragenter = function(e) {
                if(e.dataTransfer.types.length < 4){
                    el.show();
                }
            };
            el[0].ondrop = function() {
                if(jQuery('.photoThumbContainer').length > 0){
                    el.hide();
                }
            };
        }
    });

    //FILES ADDED HANDLER 2 (called after a drop of files)
    uploader.bind('FilesAdded', function(up, files){
        var i = 0;
        plupload.each(files, function(file) {
            file.index = i;
            uploader.fileAdded(file);  
            i++;
        });
        uploader.start();
    });

    //inject the file into the interface (before before upload function)
    uploader.fileAdded = function(file){
        var processId           = process.createProcessId(file.index);
        //added to the file object and passed into the registration
        file.processId          = processId;
    };

    //BEFORE EACH UPLOAD HANDLER 3
    //Note: this has to be here for any variable passed that changes per file. For example the processId.
    uploader.bind('BeforeUpload',function(up,file){
        var processId       = file.processId;
        var photoDiv        = jQuery('#photo'+processId+' .photoThumbDiv');
        var image           = jQuery(new Image()).appendTo(photoDiv);
        var preloader       = new mOxie.Image();

        preloader.downsize(100,100);
        preloader.onload    = function() {
            var img         = preloader.getAsDataURL();
            jQuery('#photo'+processId+' .photoThumbDiv').html('<img src="'+img+'">');
        };
        preloader.load(file.getSource());

        uploader.settings.multipart_params.processId        = file.processId;
    });

    //PROGRESS HANDLER 4
    uploader.bind('UploadProgress',function(up,file){
        var processId   = file.processId;
        var pb          = collection.getProgressBar(processId);
        if(file.percent<100){
            var progress = file.percent;
            pb.html('<div class="progressBar"><div class="progressBarColor"> </div></div>');
            jQuery('#photo'+processId+' .progressBarColor').css({width:progress+'%'});
        }else{
            pb.html('<div class="loaderSmall"></div> Processing');
        }
    });

    //FILE FINISHED HANDLER 5 
    uploader.bind('FileUploaded',function(up,file,info){
        var processId       = file.processId;
        var fileName        = file.name;
        file.fileName       = file.name;
        var params          = file;
        delete params.getNative;
        delete params.getSource;
        delete params.destroy;
        ui.request('registerFile','Photo',params);

        //ALL COMPLETE
        if(uploadedCnt == up.files.length){
            //uploader.splice();
            uploader.refresh();
        }
    });

    //GENERAL ERROR HANDLER -
    uploader.bind('Error', function(up, err) {
        switch(err.code){
            case plupload.FILE_EXTENSION_ERROR:
                fileExtensions = '';
                jQuery.each(up.settings.filters.mime_types,function(index,up) {
                    if(fileExtensions==''){
                        fileExtensions=up.extensions;
                    }else{
                        fileExtensions+=","+up.extensions;
                    }
                });
                var isOrAre = (fileExtensions && fileExtensions.indexOf(',') !== -1) ? 'are':'is';
                alertBox.msg("Only "+fileExtensions+" "+isOrAre+" accepted. Please try again.");
                break;
            case plupload.FILE_SIZE_ERROR:
                alertBox.msg("File size can not be larger than "+up.settings.max_file_size+'.');
                break;
        }
        uploader.refresh(); 
    });

    uploader.init();
},

Any input is appreciated. 任何输入表示赞赏。 Thanks. 谢谢。

If anyone is interested in the solution here, it is because uploader was being set globally. 如果有人对这里的解决方案感兴趣,那是因为正在全局设置上载器。 Adding 'var' in front of uploading whenever initializing the uploader fixed it. 每当初始化上传器修复它时,在上传之前添加“ var”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Plupload访问服务器上的multipart_params值? - How do I access the multipart_params values on the server with Plupload? plupload 动态多参数 - plupload dynamic multi params 在调用函数中重新初始化数组 - Re-initialization array in called function jQuery DataTable设置重新初始化 - jQuery DataTable settings re-initialization 是否可以防止在 Cordova 中重新初始化应用程序? - Is it possible to prevent app re-initialization in Cordova? 如何在我的Vue.js代码中删除重复的重新初始化代码 - How to remove the duplicated re-initialization code at my vuejs code 重新初始化后,下拉列表未在Internet Explorer中显示选择 - Dropdownchecklist not showing selection in internet explorer after re-initialization 即使在函数中重新初始化全局变量后,它也会返回 null - global variable returns null even after re-initialization it in a function 第一个jQuery插件-如何在重新初始化时处理事件? - First jQuery plugin- How to handle events on re-initialization? 如何像我们在 c 中那样在 JavaScript 中创建一个 static 变量? (避免重新初始化变量) - How to make a static variable in JavaScript like we do in c ? ( to avoid re-initialization of the variable)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM