post請求匯出Excel表格
阿新 • • 發佈:2018-11-10
axios.
interceptors.
response.
use((
response)
=>{
if(
response.
config &&
response.
config.
responseType ==
'blob') {
const
blob =
new
Blob([
response.
data], {
type:
'application/x-msdownload' });
//後臺需要相同的type
let
filename =
`
${
name
}
.xls`;
if (
'download'
in
document.
createElement(
'a')) {
const
downloadElement =
document.
createElement(
'a');
let
href =
'';
if(
window.
URL)
href =
window .
URL.
createObjectURL(
blob);
else
href =
window.
webkitURL.
createObjectURL(
blob);
downloadElement.
href =
href;
downloadElement.
download =
filename;
document.
body.
appendChild(
downloadElement);
downloadElement .
click();
if(
window.
URL)
window.
URL.
revokeObjectURL(
href);
else
window.
webkitURL.
revokeObjectURL(
href);
document.
body.
removeChild(
downloadElement);
}
else {
navigator.
msSaveBlob(
blob,
filename);
}
return
Promise.
resolve(
response.
data);
}
return
response;
})
const
defaultConfig = {
baseURL:
'',
mode:
'cors',
headers: {
// "your-content": 'application/x-msdownload',
"Accept"
:
"application/json",
"Content-Type"
:
"application/json;charset=utf-8"
},
responseType:
'json'
}
const
post2 = (
url,
data,
config)
=> {
return
axios.
post(
url,
data,
Object.
assign({},
defaultConfig,
config))
}
https.
post2(
this.
apiName.
exportMealGroupByDateAndShop,
data,{
responseType:
'blob' })