【问题标题】:Can´t open word file with XMLHttpRequest无法使用 XMLHttpRequest 打开 word 文件
【发布时间】:2022-03-28 00:13:21
【问题描述】:

我试图用 javascript 打开一个带有 XMLHttpRequest 的 word 文件,但我不能。 任何人都可以帮助解决它吗? 我使用的代码是:

无题 5

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">

<head>
<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />
<title>Untitled 5</title>
<script type=\"text/javascript\">


    try{
        var abreficheiro = new XMLHttpRequest();                   
        abreficheiro.open(\'GET\', \'cir_entidade.doc\');
        abreficheiro.setRequestHeader(\'Content-disposition\', \'attachment\');         
        abreficheiro.setRequestHeader(\'Content-type\', \'application/msword\');            
        abreficheiro.send();
        }
catch(err) {
        var strErr = \'Error:\';
        strErr +=\'\\\\nNumber:\'+err.number;
        strErr +=\'\\\\nDescription:\'+err.description;
        document.write(strErr);
    }

</script>
</head>

<body>

</body>

</html>

    标签: ms-word xmlhttprequest document


    【解决方案1】:

    我找到了这种方法 只需将 ${filename} 替换为文件名即可。 适用于镀铬和边缘

            var request = new XMLHttpRequest();
            request.open("GET", "${filename}.doc");
            request.responseType = "blob";
            request.onload = function() {
              // set `blob` `type` to `"text/html"`;
              var blob = new Blob([this.response], {type:"application/msword"});
              var url = URL.createObjectURL(blob);
              var w = window.open(url);
            }
            request.send();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2019-01-06
      相关资源
      最近更新 更多