1

请我在使用 edgee:slinghot 包上传到 Amazon S3 之前尝试修改用户发布的选定文件的文件名。我可以很好地上传文件,但问题是我如何修改文件名。我通过将修改后的名称保存到变量中在客户端上对其进行了修改。我现在的问题是如何访问在服务器环境中的客户端上声明和保存的变量。我似乎无法绕过它。

    'change .js-submitTeamPaper' : function(event , template){

       event.preventDefault();
       let paper = template.paperDetails.get();
       newFilename = paper[0].paper_name + "_"
       _.map(paper[0].member , (member)=>{
        newFilename +=  "_" + member.regnum + "_"
      });
      newFilename += paper[0]._id;
      let file = event.target.value;
      let fileArray = file.split(".");
      let ext = fileArray[fileArray.length - 1];
      newFilename += "." + ext;

   studentFileUpload(event , template , 'submitTeamTermPaper' ,  'divProgress');

    }

上传文件的代码。

 let _collectfile = (event , template) =>{
    let file = event.target.files[0]
    return file
  }

  let _showProgressBar = (div) => {
    let _div = document.getElementById(div);
   _div.classList.remove("hide");
  }

 let _closeProgressBar = (div) => {
   let _div = document.getElementById(div);
  _div.classList.add("hide");
 }

  let _slingShotUploadConfigure = (event , template , folder ,div) => {
      let _upload = new Slingshot.Upload(folder);
      let _file = _collectfile(event , template);
      _showProgressBar(div);
      _upload.send(_file , (error , downloadUrl) => {
     template.uploader.set();
      if (error){
        //throw new Meteor.Error('500' , error.reason);
        event.target.value = '';
        sAlert.error(error.reason , {effect: 'bouncyflip',
        position: 'bottom-right', timeout: 3000, onRouteClose: false, stack: false, offset: '150px'});
        _closeProgressBar(div);
     }
     else{
       sAlert.success('File was uploaded successfully' , {effect: 'genie',
       position: 'bottom-right', timeout: 3000, onRouteClose: false, stack: false, offset: '150px'});
       event.target.value = '';
       template.downloadUrl.set(downloadUrl);
       _closeProgressBar(div);
       //return downloadUrl;
     }
 });
 template.uploader.set(_upload);

}

 export default function(event , template , folder ,div , progress){
   return _slingShotUploadConfigure(event , template , folder,div , progress)
 }

然后我从 '../../modules/handle-fileuploads' 将模块作为 studentFileUpload 导入;下面是用于上传的流星弹弓代码

   Slingshot.createDirective("submitTeamTermPaper", Slingshot.S3Storage, {
     bucket: Meteor.settings.BucketName,
     AWSAccessKeyId : Meteor.settings.AWSAccessKeyId,
     AWSSecretAccessKey : Meteor.settings.AWSSecretAccessKey,
     acl: "public-read",

     authorize: function () {
     // do some validation
     // e.g. deny uploads if user is not logged in.
       if (this.userId) {
         return true;
     }
   },

   key: function (file) {
   //file here is the file to be uploaded how do i get the modified file name i defined in the client as newFilename here
   let timeStamp = + new Date;
//let newFilename = file.name.replace(/_/g , "-");
return  'Team_Term_Papers/' + timeStamp + '_' + '_' + newFilename;

} }); 从我的代码中,newFilename 是保存修改后的文件名的变量。我如何从服务器环境访问它。非常感谢任何帮助。谢谢

4

1 回答 1

1

您可以使用元上下文将额外信息传递给 slingshot:

metacontext = {newName: "foo"};
let _upload = new Slingshot.Upload(folder,metacontext);

key然后你可以在你的函数中访问那个元上下文:

key: function (file,metacontext) {
  let timeStamp = + new Date;
  let newFilename = metacontext ? metacontext.newName : file.name;
  newFilename = newFilename.replace(/_/g , "-");
  return  'Team_Term_Papers/' + timeStamp + '_' + '_' + newFilename;
}
于 2017-03-06T07:12:15.133 回答