fetch('Test/aaa/bbb', {
method: 'POST',
body: formData,
cache: 'default'
}).then(response => {
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.includes('application/json')) {
var result = response.json();
result.then(value => {
alert(value.Message)
});
return; // 阻止后续的执行
} else {
return response.blob(); // 或者其他处理二进制数据的方法
}
})
.then(blob => {
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', getFormattedTime()+'.zip'); // 设置下载文件的文件名,可以根据后端返回的文件名进行设置
document.body.appendChild(link);
link.click();
}).then(data => {
}).catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
用fetch可以完美解决问题