你应该把请求的Accept设置成application/octet-stream.
这其实有的奇怪, 因为一般情况下 Excel 文件的 Accept 是application/vnd.ms-excel.
但是由于 flasgger 是使用 swagger-ui 作为前端, 而 Swagger 发送文件类请求是使用 superagent, 使用的是 blob 技术, 需要设置 xhr 的 ResponseType 为blob.
我们看flasgger_static/lib/http.js:
if(this.binaryRequest(accept)) {
r.on('request', function () {
if(this.xhr) {
this.xhr.responseType = 'blob';
}
});
}
SuperagentHttpClient.prototype. binaryRequest = function (accept) {
if(!accept) {
return false;
}
return (/^image/i).test(accept)
|| (/^application\/pdf/).test(accept)
|| (/^application\/octet-stream/).test(accept);
};
只在 Accept 匹配为image/i,/^application\/pdf/,/^application\/octet-stream/, 才将 responseType 设置成 blob
所以要么将/^application\/vnd.ms-excel/这个匹配加入h