Http Header for Downloading Microsoft Word and Excel Files

http header for downloading Microsoft Word and Excel files

Change this

header('Content-Type: application/msword');

to

header('Content-Type: application/octet-stream');

EDIT:

And change

header("Content-Disposition: attachment; filename='$No_$Name_$Test.docx");

to

header("Content-Disposition: attachment; filename=\"{$No}_{$Name}_{$Test}.docx\"");

Unable to open export excel file on hiding response headers using URL Rewrite

Okay so I was able to block all but Server response headers and I will share what worked for me so far:

X-AspNet-Version :

Just add the attribute enableVersionHeader="false" to <httpRuntime/>. It should look as follows :

<system.web>
<httpRuntime enableVersionHeader="false" />
</system.web>

X-Powered-By:

  1. Open IIS
  2. Select the Sitename under Sites folder
  3. Select HTTP Response Headers under IIS
    response-headers-module
  4. Select X-Powered-By and remove the existing value and add an empty string.

X-Powered-By

Note: I will update once I am able to figure out a way to block server headers as well on this answer.

How to download excel (.xls) file from API in postman?

Try selecting send and download instead of send when you make the request. (the blue button)

Screenshot showing "Send and Download" button in postman

https://www.getpostman.com/docs/responses

"For binary response types, you should select Send and download which will let you save the response to your hard disk. You can then view it using the appropriate viewer."

How to download files using axios

When response comes with a downloadable file, response headers will be something like

Content-Disposition: "attachment;filename=report.xls"
Content-Type: "application/octet-stream" // or Content-type: "application/vnd.ms-excel"

What you can do is create a separate component, which will contain a hidden iframe.

  import * as React from 'react';

var MyIframe = React.createClass({

render: function() {
return (
<div style={{display: 'none'}}>
<iframe src={this.props.iframeSrc} />
</div>
);
}
});

Now, you can pass the url of the downloadable file as prop to this component, So when this component will receive prop, it will re-render and file will be downloaded.

Edit: You can also use js-file-download module. Link to Github repo

const FileDownload = require('js-file-download');

Axios({
url: 'http://localhost/downloadFile',
method: 'GET',
responseType: 'blob', // Important
}).then((response) => {
FileDownload(response.data, 'report.csv');
});

Download MS Excel/Word via AngularJS call to a REST service

This is the Controller function I ended up using:

@RequestMapping(value = "/downloadDocx", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.CREATED)
public void downloadDocx(@RequestBody DocxInputBean docxInput,
HttpServletResponse response) throws Exception {

File docxFile = outputManager.createDocxProfile(docxInput);

response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");

InputStream is = new FileInputStream(docxFile);
org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
is.close();
}


Related Topics



Leave a reply



Submit