完成Filestack进程后执行函数

时间:2018-03-16 17:56:40

标签: javascript jquery html filestack

我使用Filestack v3,我想比较DOC / DOCX文件的字数和页数。前提是:"每页有200个单词"我想制定一个例子:"如果一个doc / docx文件有2个页面,理论上该文件有400个单词,如果它有400个或400个以上的单词就行了,否则就错了#34;

所以,我有这个:

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");
   }
}

我想要一个名为启发的功能与输入值进行比较并在控制台中显示结果(页数,单词数和最终消息),但问题是启发函数在Filestack进程完成之前执行,我希望Filestack进程首先在输入上放置值然后执行函数。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

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