How to handle binary response with Collection Runner

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
i need to download video file (.ts) from collection runner.
i cloned the responsetofile-postman repo
here is my script.js

const express = require('express'),
  app = express(),
  fs = require('fs'),
  shell = require('shelljs'),

   // Modify the folder path in which responses need to be stored
  folderPath = './Responses/',
  defaultFileExtension = 'ts', // Change the default file extension
  bodyParser = require('body-parser'),
  DEFAULT_MODE = 'writeFile',
  path = require('path');

// Create the folder path in case it doesn't exist
shell.mkdir('-p', folderPath);

 // Change the limits according to your response size
app.use(bodyParser.json({limit: '50mb', extended: true}));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })); 

app.get('/', (req, res) => res.send('Hello, I write data to file. Send them requests!'));

app.post('/write', (req, res) => {
  let extension = req.body.fileExtension || defaultFileExtension,
    fsMode = req.body.mode || DEFAULT_MODE,
    uniqueIdentifier = req.body.uniqueIdentifier ? typeof req.body.uniqueIdentifier === 'boolean' ? Date.now() : req.body.uniqueIdentifier : false,
    filename = `${req.body.requestName}${uniqueIdentifier || ''}`,
    filePath = `${path.join(folderPath, filename)}.${extension}`,
    options = req.body.options || undefined;
  
  fs[fsMode](filePath, req.body.responseData, options, (err) => {
    if (err) {
      console.log(err);
      res.send('Error');
    }
    else {
      //console.log(req.body.responseData)
      res.send('Success');
    }
  });
});

app.listen(3000, () => {
  console.log('ResponsesToFile App is listening now! Send them requests my way!');
  console.log(`Data is being stored at location: ${path.join(process.cwd(), folderPath)}`);
});

and here is my tests script in postman collection

// The opts for the server, also includes the data to be written to file
let opts = {
    requestName: request.name || request.url,
    fileExtension: 'ts',
    mode: 'writeFile', // Change this to any function of the fs library of node to use it.
    uniqueIdentifier: false,
    responseData: pm.response
};

pm.sendRequest({
    url: 'http://localhost:3000/write',
    method: 'POST',
    header: 'Content-Type:application/json',
    body: {
        mode: 'raw',
        raw: JSON.stringify(opts)
    }

}, function (err, res) {
    console.log(res);
});

when i click the run button, a video file(ts) is saved in the path ‘ResponseToFile-Postman\Responses’
but the file is not right , this file’s size is 4.33m ,and can not be played in video player
002

when i download the response by ‘send and dowload’ button ,the file is correct , size is 2.34m, and can be played in video player

here is my import file for postman

{
	"info": {
		"_postman_id": "c04cf8ae-9860-4caf-a123-625fe5f703a7",
		"name": "episode2",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "002",
			"protocolProfileBehavior": {
				"disabledSystemHeaders": {
					"connection": true,
					"user-agent": true
				}
			},
			"request": {
				"method": "GET",
				"header": [
					{
						"key": "authority",
						"value": "t.wdubo.com",
						"type": "text"
					},
					{
						"key": "method",
						"value": "GET",
						"type": "text"
					},
					{
						"key": "scheme",
						"value": "https",
						"type": "text"
					},
					{
						"key": "accept-language",
						"value": "zh-CN,zh;q=0.9,en;q=0.8",
						"type": "text"
					},
					{
						"key": "origin",
						"value": "https://m.duboku.fun",
						"type": "text"
					},
					{
						"key": "referer",
						"value": "https://m.duboku.fun/",
						"type": "text"
					},
					{
						"key": "user-agent",
						"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
						"type": "text"
					}
				],
				"url": {
					"raw": "https://v.zdubo.com/20211215/lyEJ5Nhc/hls/tyKYGEaE.ts",
					"protocol": "https",
					"host": [
						"v",
						"zdubo",
						"com"
					],
					"path": [
						"20211215",
						"lyEJ5Nhc",
						"hls",
						"tyKYGEaE.ts"
					]
				}
			},
			"response": []
		}
	],
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"exec": [
					"// The opts for the server, also includes the data to be written to file",
					"let opts = {",
					"    requestName: request.name || request.url,",
					"    fileExtension: 'ts',",
					"    mode: 'writeFile', // Change this to any function of the fs library of node to use it.",
					"    uniqueIdentifier: false,",
					"    responseData: pm.response.text()",
					"};",
					"",
					"pm.sendRequest({",
					"    url: 'http://localhost:3000/write',",
					"    method: 'POST',",
					"    header: 'Content-Type:application/json',",
					"    body: {",
					"        mode: 'raw',",
					"        raw: JSON.stringify(opts)",
					"    }",
					"}, function (err, res) {",
					"    console.log(res);",
					"});"
				]
			}
		}
	]
}

please help me ,thanks