Change Content-Type header when responding with WebApp


#1

On the server I have something like:

WebApp.connectHandlers.use('/export', (req, res, next) => {
    ...
    fs.readFile(file, (err, buf) => {
        ...
        res.writeHead(200, {
          'Content-Type': 'application/octet-stream',
          'Content-Disposition': `attachment; filename="data.xlsx"`
        });
        res.end(buf);
    });
}

On the client, when downloading the file, I get a warning:

Resource interpreted as Document but transferred with MIME type application/octet-stream

Checking the response headers in the chrome devtools network tab, I noticed that the content-type header is set to text/html; charset=utf-8. Why is that?


#2

We used the npm send package to do just that.

          send(req, filePath, {acceptRanges:true, cacheControl:false}).on('headers', function(res) {
            res.setHeader('Access-Control-Allow-Origin', '*');
            res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
            res.setHeader('Content-Security-Policy', 'frame-src *');
            res.setHeader('Vary', 'Origin');
            res.setHeader('Expires', 'Thu, 26 Apr 2018 02:28:16 GMT');
          }).pipe(res);

#3

I got the same warning when using that package. I’ve only tested in development thus far.