Javascript Plupload 添加文件时自动开始上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8137229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:50:41  来源:igfitidea点击:

Plupload Automatically start upload when files added

javascriptruby-on-rails-3paperclipplupload

提问by Abid

When files are added i want to start the upload process automatically. I called the start function at the end of FilesAdded but it doesn't start the upload.

添加文件后,我想自动启动上传过程。我在 FilesAdded 的末尾调用了 start 函数,但它没有开始上传。

uploader.bind('FilesAdded', function(up, files) {
      var str = "";
      for (var i in files) {
        str += '<div id="' + files[i].id + '">' + files[i].name + ' (' + plupload.formatSize(files[i].size) + ') <b></b></div>';
      }
      $('#filelist').html(str);
      up.refresh();
      up.start();
    });

Here is my creation code

这是我的创建代码

var uploader = new plupload.Uploader({
      runtimes: 'html5,flash,silverlight',
      autostart : true,
      url: '<%= images_path %>',
      max_file_size: '10mb',
      multipart: true,
      browse_button: "pickfiles",
      container: "the-uploader",
      drop_element : "drop-area",  
      multipart_params: {
        '_http_accept': 'application/javascript',
        '<%=request_forgery_protection_token%>': '<%=form_authenticity_token%>',
        '<%=request.session_options[:key]%>': '<%=request.session_options[:id]%>'
      },
      filters: [
        {title: "Images", extensions: "avi,jpg,jpeg,png,zip"}
      ],
    });

回答by ingh.am

Adding up.start()in your FilesAdded bind should start the upload when a file is added. I've gone down the route of calling my uploader like so (I had problems doing it the way you are trying to call it):

添加up.start()FilesAdded 绑定应该会在添加文件时开始上传。我已经走上了像这样调用我的上传者的路线(我在按照您尝试调用它的方式进行操作时遇到了问题):

$(function() {
    // Setup html5 version
    $("#html5_uploader").pluploadQueue({
      // General settings
      runtimes : 'html5',
      url : 'upload.php',
      max_file_size : '10mb',
      chunk_size : '1mb',
      unique_names : true,
      dragdrop : true,
      multiple_queues : false,
      multi_selection : false,
      max_file_count : 1,

      // Specify what files to browse for
      filters : [
        {title : "Text files", extensions : "txt"}
      ],

      init : {
        FilesAdded: function(up, files) {
          up.start();
        },
        UploadComplete: function(up, files) {
          $.each(files, function(i, file) {
            // Do stuff with the file. There will only be one file as it uploaded straight after adding!
          });
        }
      }
    });
  });

回答by Stefan Ladwig

"Make sure you bind it after the init since it binds default handlers."

“确保在 init 之后绑定它,因为它绑定了默认处理程序。”

So your code:

所以你的代码:

uploader.bind('FilesAdded', function(up, files) {...});

after your

在你之后

uploader.init();

more information

更多信息

回答by Gafitescu Daniel

For me it didn't work your version but did work :

对我来说,它对您的版本无效,但确实有效:

FilesAdded: function(up, files) {
   setTimeout(function () { up.start(); }, 100);
 },

So set a timer after 100 ms to execute the start. I am using the jquery ui version while testing and got this error :

所以在100ms后设置一个定时器来执行启动。我在测试时使用 jquery ui 版本并收到此错误:

g("#" + l.id).position() is null

/js/plupload/js/jquery.ui.plupload/jquery.ui.plupload.js

回答by NullPointer

I was also wanting same for me and found below way to do it.

我也想要同样的东西,并在下面找到了这样做的方法。

A new way to automatically start file uploading after adding file is just need to set true to autostartproperty like below

添加文件后自动开始上传文件的新方法只需将 true 设置为autostart如下所示的属性

$("#uploader").plupload({
    .....
    autostart: true,
    .....
});

回答by Azam Alvi

Just trigger the Start Upload button by this way

只需通过这种方式触发开始上传按钮

FilesAdded: function(up, files) {
   $('#fileupload_start').click();
},

This will upload the file without waiting for 100 ms.

这将上传文件而无需等待 100 毫秒。