【问题标题】:fs.writeFile is not returning pdf file as it's content isfs.writeFile 没有返回 pdf 文件,因为它的内容是
【发布时间】:2017-03-02 08:30:58
【问题描述】:

我正在使用 ajax 从某个链接(基本上是锚 href)获取一些文件,然后我将内容发送到节点服务器,如下所示:

<a class="myTag" href="/getFiledata?uid=7674&&pass=75876789">Download</a>

.

jQuery.get(jQuery("a.myTag").attr("href"), function(data, status){
            console.log("typeof data: ",typeof data);
            jQuery.ajax({
                url : 'https://7c099919.ngrok.io/fildta?fileTyp=naukriDta',
                type : 'POST',
                contentType: 'application/json',
                dataType: 'json',
                data : JSON.stringify({bdy:data}),
                arrayKey:'',
                processData: false,  // tell jQuery not to process the data
                success : function(data) {
                    console.log(data);
                }
            });
            console.log("Data: ",data );
        }); 

我在服务器上列出和写入数据如下:

function(req, res, next){
                var fd=path.join(__dirname,"../upld/tmp.pdf");
                console.log("req body:  ",JSON.stringify(req.body));
                fs.writeFile(fd, req.body.bdy, function(err) {
                    if (err){
                        console.log(fd,"\n\n\n\n\n Can not write to above file:\n\n",err);
                    }else {
                        console.log(fd,' is Done');
                    }
                });
                console.log("req query:  ",req.query);
                res.send({"msg":"File is uploaded"});
            }

但是当我使用任何阅读器阅读创建的 pdf 文件时,它显示为空白,但是当我使用 notepad++ 阅读其内容时,内容与客户端上的内容相同。

文件内容如下:

Data: %PDF-1.5
%����
24 0 obj
<<
/Linearized 1
/L 121184
/H [ 1971 370 ]
/O 26
/E 98912
/N 4
/T 120577
>>
endobj

xref
24 70
0000000017 00000 n
0000001869 00000 n
0000002341 00000 n
0000002744 00000 n
0000002924 00000 n
0000003187 00000 n
0000003532 00000 n
0000003720 00000 n
0000003999 00000 n
0000008165 00000 n
0000008222 00000 n
0000008397 00000 n
0000008655 00000 n
0000008884 00000 n
0000009034 00000 n
0000009064 00000 n
0000009246 00000 n
0000009328 00000 n
0000009601 00000 n
0000018049 00000 n
0000018198 00000 n
0000018570 00000 n
0000018753 00000 n
0000019026 00000 n
0000025387 00000 n
0000025414 00000 n
0000025569 00000 n
0000025599 00000 n
0000025786 00000 n
0000026064 00000 n
0000036489 00000 n
0000036867 00000 n
0000037320 00000 n
0000037509 00000 n
0000037787 00000 n
0000045277 00000 n
0000045492 00000 n
0000045642 00000 n
0000045672 00000 n
0000045854 00000 n
0000046128 00000 n
0000061832 00000 n
0000062215 00000 n
0000062645 00000 n
0000062828 00000 n
0000063102 00000 n
0000069388 00000 n
0000069443 00000 n
0000069592 00000 n
0000069622 00000 n
0000069803 00000 n
0000070073 00000 n
0000073366 00000 n
0000073411 00000 n
0000073718 00000 n
0000073873 00000 n
0000073903 00000 n
0000074090 00000 n
0000074369 00000 n
0000083843 00000 n
0000084123 00000 n
0000084551 00000 n
0000084699 00000 n
0000084729 00000 n
0000084909 00000 n
0000085193 00000 n
0000093386 00000 n
0000093570 00000 n
0000093941 00000 n
0000001971 00000 n
trailer
<<
/Size 94
/Prev 120566
/Info 23 0 R
/Root 25 0 R
/ID [<4e891be7c450bedc9528eba318fe1823><4e891be7c450bedc9528eba318fe1823>]
>>
startxref
0
%%EOF

25 0 obj
<<
/Type /Catalog
/Pages 22 0 R
/Lang (en-IN)
/MarkInfo << /Marked true >>
>>
endobj
93 0 obj
<<
/S 188
/Filter /FlateDecode
/Length 280
>>
stream
x�c```b``������*� Ȁ

我对 MIME-TYPE 和文件内容概念很幼稚。我想在写文件之前我想在某个地方设置某些数据(MIME-TYPE、内容类型等)。

基本上我的目标是将从锚点获取的文件重定向到我的节点服务器。

【问题讨论】:

  • 将 URL 发送到服务器并让服务器下载文件而不是下载并重新上传文件不是更有意义吗?
  • 该页面在发送文件之前使用了一些 cookie、referer 等,因此如果请求来自服务器,它会阻塞
  • 尝试使用XMLHttpRequest2 将文件下载为Blob(现在大多数浏览器support xhr2)。当你有一个 blob 时,你可以轻松上传,而不会遇到任何字符编码问题。
  • 是的,它奏效了。谢谢。
  • 如果您在自己的答案中分享代码,我会投赞成票。 :)

标签: jquery node.js mime-types content-type fs


【解决方案1】:

@Tomalak 建议使用 xhr2。我刚刚将 responseType/dataType 添加到 blob/binary,具体取决于它是否有效。

客户端代码如下: - 第一种方法:

var xhr = new XMLHttpRequest();
   xhr.open('GET', jQuery("a.myTag").attr("href"), true);
   xhr.responseType = 'blob';

   xhr.onload = function(e) {
       if (this.status == 200) {
           // get binary data as a response
           var blob = this.response;
           var base64data;
           var reader = new window.FileReader();
           reader.readAsDataURL(blob);
           reader.onloadend = function() {
               base64data = reader.result;
               var myDta={bdy:base64data};
               console.log("myDta",myDta);
               jQuery.ajax({
                   url : 'https://7c099919.ngrok.io/fildta?fileTyp=naukriDta',
                   type : 'POST',
                   data : myDta,
                   success : function(data) {
                       console.log(data);
                   }
               });
           };
       }
   };

   xhr.send();

第二种方法

$.ajax({
  url: jQuery("a.myTag").attr("href"),
  type: "GET",
  dataType: "binary",
  processData: false,
  success: function(result){
      // same stuffs with binary data as above
  }
});

服务器代码如下:

function(req, res, next){
                var fd=path.join(__dirname,"../upld/tmp.pdf");
                console.log("req body:  ",JSON.stringify(req.body));
                var base64Data = req.body.bdy.replace(/^data:application\/pdf;base64,/, "");
                console.log("base64Data  ",base64Data);
                fs.writeFile(fd, base64Data,'base64', function(err) {
                    if (err){
                        console.log(fd,"\n\n\n\n\n Can not write to above file:\n\n",err);
                    }else {
                        console.log(fd,' is Done');
                    }
                });
                console.log("req query:  ",req.query);
                res.send({"msg":"File is uploaded"});
            }

哇哦..它成功了:)

严格来说,XHR2 不是 HTML5。然而,它是其中的一部分 浏览器供应商正在对核心进行渐进式改进 平台。

【讨论】:

    猜你喜欢
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2022-11-03
    相关资源
    最近更新 更多