【问题标题】:Execute function after finishes a Filestack process完成 Filestack 进程后执行函数
【发布时间】:2018-08-25 21:10:50
【问题描述】:

我正在使用 Filestack v3,我想比较 DOC/DOCX 文件的字数和页数。前提是:“每页有 200 个字”我想表述为:“如果一个 doc/docx 文件有 2 页,理论上该文件有 400 个字,如果它有 400 个或超过 400 个字是好的,否则是错误的”。

所以,我有这个:

HTML

<html>
<body>

<button id="files">UPLOAD</button>
<textarea id="dkd"></textarea>
<div id="result"></div>
<input type="text" id="cnw">
<input type="text" id="cnp">

<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://static.filestackapi.com/v3/filestack.js"></script>
</body>
</html>

而我的 JS 代码是:

$("#files").click(function(){
  var client = filestack.init('myAPI');

  client.pick({
    accept: ['image/jpeg','image/png','image/gif','application/pdf','application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
    fromSources: ['local_file_system','googledrive','gmail','facebook','dropbox','onedrive','webcam'],
    lang: 'es',     
    maxSize: 2097152,
    maxFiles: 100
  }).then(function(Blobs) {

  var result = JSON.parse(JSON.stringify(Blobs));

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

    if(result.filesUploaded[i].mimetype=="application/msword" || result.filesUploaded[i].mimetype=="application/vnd.openxmlformats-officedocument.wordprocessingml.document"){

        var dataFile = "https://cdn.filestackcontent.com/output=docinfo:true/"+result.filesUploaded[i].handle;
        var handlee = result.filesUploaded[i].handle;
        var lcconvert = "https://process.filestackapi.com/output=format:txt/"+result.filesUploaded[i].handle;

        //GET NUMBER OF WORDS
        $("#dkd").load(lcconvert, function(){               
            $.post("load2.php",{
                url: $("#dkd").val()
            }).success(function(response){

                document.getElementById('result').innerHTML += "<input type='hidden' name='mswcount' value='"+response+"'>";
                var arr = document.getElementsByName('mswcount');
                var tot=0;
                for(var i=0;i<arr.length;i++){
                    if(parseInt(arr[i].value))
                        tot += parseInt(arr[i].value);

                }                       

                document.getElementById('cnw').value = tot;

            });
        });

        //GET NUMBER OF PAGES
        $.post("numpage.php",{
            dF: dataFile
        }).success(function(response){

            var numpages = $.trim(response);// number of pages
            $("#cnp").val(numpages);

        });//post
    }//if
  }//for

  inspire();

 });//client.pick
});//files


//COMPARISON
function inspire(){

   var pag = $("#cnp").val();//pages
   var pagpal = pag*200; //number of pages * 200 = number of words
   var pal = $("#cnw").val();//words

   console.log("Pages: "+pag);
   console.log("Words: "+pal);

   if(pal>=pagpal){
      console.log("OK");
   } else {
      console.log("WRONG");
   }
}

我希望名为 inspire 的函数与输入值进行比较并在控制台中显示结果(页数、字数和最终消息),但问题是 inspire 函数在 Filestack 进程完成之前执行,我希望 Filestack 进程首先完成输入值,然后执行函数。

我该如何解决?

【问题讨论】:

    标签: javascript jquery html filestack


    【解决方案1】:

    将要运行的代码放入client.pick(/**/).then() 回调中。或者使用await/async等待client.pick返回值:https://javascript.info/async-await

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2014-06-10
      • 2018-10-30
      • 2018-04-05
      • 2021-11-11
      相关资源
      最近更新 更多