V
V
Vladimir Golub2022-03-23 14:02:08
Node.js
Vladimir Golub, 2022-03-23 14:02:08

How to send an object with data back to a Get request so that it is recognized as a file?

Processing the request on the server, I prescribe the headers, but it opens in the browser, and is not downloaded as a file. I want to open a dialog, save as.

server

@Get()
  getHello(@Response() res) {

    const data = {
      firstName: 'Jack',
      lastName:  'Ivanov'
    }

    return res
      .writeHeader('Content-Type', 'text/json')
      .writeHeader('Content-Disposition', 'inline; filename="123.json"')
      .write(data)
      .end();
  }


client
<a href="http://localhost:3000/" target="_self">Загрузить файл</a>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Golub, 2022-03-23
@RazerVG

Need to use attachment

@Controller('file')
export class FileController {
  @Get('/')
  @Header('Content-Type', 'application/octet-stream')
  @Header('Content-Disposition', 'attachment; filename="123.json"')
  getFile() {
    return {
      name: 'Jack'
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question