需要访问后端获取文件流,例如excel或视频等
使用axios设置responseType为arraybuffer
返回的文件结果并不是arraybuffer结构
使用源生axios(未拦截request/response)也不行..
使用$http也不行...
但是同一个后台
不使用vue-element-admin,而使用源生的vue框架则没问题
排除后台/axios问题,猜测是vue-element-admin某个地方导致responseType未生效?
请求代码
export function fetchFile(data, requestType = 'arraybuffer') {
return request({
url: '/download/excel',
method: 'post',
responseType: requestType,
data: { param: data}
})
}
服务端
$app->get('/download/excel', function($param) use ($app) {
$file = '../download/test.xls';
$filename= 'test.xls';
header('Access-Control-Allow-Origin:*');
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel;charset=utf-8');
header("Content-Disposition: attachment; filename=". iconv('utf-8', 'gbk//ignore', $filename));
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
$fp = fopen($file, 'rb');
ob_get_clean();
header("Content-length: ". filesize($file));
fpassthru($fp);
});
返回结果
data: "��\u0011ࡱ\u001a�\u0000\u0000\u0000\u0010\u0000\u0000����\u0000\u0000...u0000\u0000����\u0000\u0000\u0000\u0000/\u0000\u0000\u0000������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������\t\u0008\u0010\u0000\u0000\u0006\u0005\u0000T8�\u0007��\u0001\u0000\u0006\u0007\u0000\u0000�\u0000\u0002\u0000�\u0004�\u0000\u0002\u0000\u0000\u0000�\u0000\u0000\u0000\\u0000p\u0000\u0002\u0000\u0001hgYm B\u0000\u0000\u0000\u0000\u0000�\u0000\u0002\u0001�[SO1\u0000\u0014\u0000,\u0001\u0001\u00006\u0000�\u0002\u0000\u0000\u0000\u0000�\u0000…"
莫非也是这个问题?
https://github.com/PanJiaChen/vue-element-admin/issues/562
mock.js在window.XMLHttpRequest层面改了request对象?
我自己试试先移除mock
已经确认。。。就是mock.js这货的问题
果然和前面
https://github.com/PanJiaChen/vue-element-admin/issues/562
所说。。。
真的是天坑呐。。。
恩,的确会导致很多问题,之后的新版本会在测试环境中用其他的mock方案。
妈呀,感谢这个问题,救命
感谢这个问题救命了,一般导致后台文件返回对乱码都是因为 responseType失效
mark
除了去除mock,还有其他的什么方案吗?
Most helpful comment
已经确认。。。就是mock.js这货的问题
果然和前面
https://github.com/PanJiaChen/vue-element-admin/issues/562
所说。。。
真的是天坑呐。。。