管道文件流的输出是什么? [英] What is the output of a piped file stream?

查看:129
本文介绍了管道文件流的输出是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这个问题没有以最好的方式措辞,但这里有更多的背景。使用GridFSBucket,我可以在mongo中存储文件并获取该文件的下载流。这是我的问题。假设我想将该文件作为对我的http请求的回复发送回去。



我这样做:

  downloadStream.pipe(RES); 

在我打印responseText的客户端,我得到一些带有一些时髦字符的长字符串看起来是加密的。这个字符串/流的格式/类型是什么?如何设置我的响应,以便我可以在客户端将流数据作为ArrayBuffer获取?



谢谢



更新:



我还没有解决问题,但是@Jekrb的建议提供了与执行控制台完全相同的输出.LOG(this.responseText)。看起来字符串不是缓冲区。以下是这两行的输出:



console.log(this.responseText.toString('utf8'))



var byteArray = new Uint8Array(arrayBuffer);





更新2 - 代码信息



前端:

  var savePDF = function(blob){
//fs.writeFile(\"test.pdf BLOB);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(this.readyState === XMLHttpRequest.DONE&& this.status === 200){

//待办事项:处理我们将要显示的响应中的文件。
console.log(this.responseText.toString('utf8'));
var arrayBuffer = this.responseText;
if(arrayBuffer){
var byteArray = new Uint8Array(arrayBuffer);
}
console.log(arrayBuffer);

}
};
xhr.open(POST,/ pdf,true);
xhr.responseType ='arrayBuffer';
xhr.send(blob);

};

BACKEND:

  app.post('/ pdf',function(req,res){

MongoClient.connect(mongodb:// localhost:27017 / test,function(err,db) ){
if(err)return console.dir(err);
console.log(Connected to Database);

var bucket = new GridFSBucket(db,{ bucketName:'pdfs'});
var CHUNKS_COLL ='pdfs.chunks';
var FILES_COLL ='pdfs.files';
//插入文件
var uploadStream = bucket .openUploadStream('test.pdf');
var id = uploadStream.id;

uploadStream.once('finish',function(){

console .log(upload finished!)

var downloadStream = bucket.openDownloadStream(id);
downloadStream.pipe(res);
});

//这将POST数据传递给文件
req.pipe(uploadStream);
});
});


解决方案

我解决了!!!



在您使用
req.responseType =arraybuffer <发出请求之前,基本上将响应类型预设为arraybuffer / p>

现在,一旦收到回复,请不要使用 responseText ,而是使用回复 response 包含带有文件数据的arraybuffer。


Perhaps the question is not worded in the greatest way but here's some more context. Using GridFSBucket, I'm able to store a file in mongo and obtain a download stream for that file. Here's my question. Let's say I wanted to send that file back as a response to my http request.

I do:

downloadStream.pipe(res);

On the client side now when I print the responseText, I get some long string with some funky characters that look to be encrypted. What is the format/type of this string/stream? How do I setup my response so that I can get the streamed data as an ArrayBuffer on my client side?

Thanks

UPDATE:

I haven't solved the problem yet, however the suggestion by @Jekrb, gives exactly the same output as doing console.log(this.responseText). It looks like the string is not a buffer. Here is the output from these 2 lines:

console.log(this.responseText.toString('utf8'))

var byteArray = new Uint8Array(arrayBuffer);

UPDATE 2 - THE CODE SNIPPETS

Frontend:

    var savePDF = function(blob){
      //fs.writeFile("test.pdf",blob);
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (this.readyState === XMLHttpRequest.DONE && this.status === 200){

            //TO DO: Handle the file in the response which we will be displayed.
            console.log(this.responseText.toString('utf8'));
            var arrayBuffer = this.responseText;
            if (arrayBuffer) {
              var byteArray = new Uint8Array(arrayBuffer);
            }
            console.log(arrayBuffer);

          }
      };
      xhr.open("POST","/pdf",true);
      xhr.responseType = 'arrayBuffer';
      xhr.send(blob);

    };

BACKEND:

app.post('/pdf',function(req,res){

  MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
  if(err) return console.dir(err);
  console.log("Connected to Database");

  var bucket = new GridFSBucket(db, { bucketName: 'pdfs' });
  var CHUNKS_COLL = 'pdfs.chunks';
  var FILES_COLL = 'pdfs.files';
  // insert file
  var uploadStream = bucket.openUploadStream('test.pdf');
  var id = uploadStream.id;

  uploadStream.once('finish', function() {

     console.log("upload finished!")

     var downloadStream = bucket.openDownloadStream(id);
     downloadStream.pipe(res);
  });

  // This pipes the POST data to the file
  req.pipe(uploadStream);
});
});

解决方案

I solved it!!!

Basically preset the response type to "arraybuffer" before you make the request using req.responseType = "arraybuffer"

Now, once you receive the response, don't use responseText, instead use response. response contains the arraybuffer with the data for the file.

这篇关于管道文件流的输出是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆