2013-03-27 46 views
1

我试图使用Filepicker.io作为上传器,但为了在有效载荷中为每个文件启动onsuccess事件我正在使用组合.pickMultiple和.store方法。像这样:Filepicker.io - 停止.pickMultiple从自动存储S3桶中的文件

filepicker.pickMultiple(function(fpfiles){ 

     for(var i = 0; i < fpfiles.length; i++){ 

      //Clean the filename 

      //Check duplicate 

      //Store the file on S3 
      filepicker.store(
       fpfiles[i].url, 
       {location: 'S3', path: 'filepicker/' + fpfiles[i].filename}, 
       function(my_uploaded_file){ 
        //Do some other cool stuff ... 
       } 
      ); 
     }    
    }); 

(这是不是使用.pickAndStore方法,整个有效载荷已完成传输之后才会触发一个事件的onSuccess)

我与此遇到的问题是,似乎以为.pickMultiple方法是'自动奇迹般地'在我的S3存储桶的根目录中保存该文件的副本;所以我最后得到了同一个文件的两个副本。

例如:

如果我上传my_file.png在我的水桶称为图像的文件夹,我应该得到的http://s3.amazonaws.com/my_bucket/IMAGES/my_file.png

发生的结果,但我也越来越: http://s3.amazonaws.com/my_bucket/UNIQUE_ID_my_file.png

任何人都知道如何防止.pickMultiple自动将文件添加到我的S3存储桶?

感谢您的任何帮助。

+0

不幸的是,在当前API中,使用.pick()或.pickMultiple()调用调用的任何“本地”上传都会将文件存储在S3存储桶中。这是由于许多依靠此行为上传至S3的传统客户。 .pickAndStore()调用很大程度上是为了解决这个问题而创建的。我知道这并不理想,但如果有必要,可以使用filepicker.remove()调用清理中间文件。 – brettcvz 2013-03-27 05:27:56

+0

嗨布雷特,在我的代码中,我会调用.remove方法吗?我尝试过实现这一点,它打破了除“重复”文件之外的所有功能 - 我试图删除的文件。 – AJB 2013-03-27 20:01:40

+0

在'.store'回调中,但在原始文件 – brettcvz 2013-03-28 00:17:10

回答

1

对于任何可能遇到同样问题的人来说,.pickMultiple() - > .store()方法是死胡同。在有效载荷中获取onSuccess事件的每个文件的(唯一)方法是使用一个vanilla <input type="file" /> onChange事件来获取元素的FILES数组,然后遍历FILES并为每个文件调用.store()在数组中。

的实施例:

$('#BTN_upload').change(function(){ 

    var files = $(this)[0].files; 

    //So you can see what should be uploading 
    console.log(JSON.stringify(files)); 

    //Loop the files array to store each file on S3 
    for(var i = 0; i < files.length; i++){ 

     //All good. Now execute the .store call 
     filepicker.store(

      //The file to upload 
      files[i], 

      //The file options 
      //(I'm storing the files in a specific folder within my S3 bucket called 'my_folder') 
      //This is also where you'll rename your file to whatever you'd like 
      {path: 'my_folder/' + files[i].name}, 

      //OnSuccess 
      function(FPFile){ 
       console.log("Store successful: ", JSON.stringify(FPFile)); 
       //Now possibly call .remove() to remove the 'temp' file from FP.io 
      }, 

      //OnError 
      function(FPError){ 
       console.log(FPError.toString()); 
      }, 

      //OnProgress 
      function(progress){ 
       console.log("Loading: " + progress + "%"); 
      } 
     ); 

    } 

}); 
filepicker.setKey('MY_FP.IO_KEY'); 

和HTML:

<input id="BTN_upload" type="file" /> 

这个例子并不是成品。您仍然需要推出自己的用户反馈(例如带进度条的队列显示),重复检查,重命名等。但这些都非常简单。

注意:这仅适用于本地到S3的上传。我不确定如何整合FP.io的其他功能。也许你会呢?