最近有个需求是需要解析一个clr文件,但是这个文件在项目里,不需要要请求获取,当然对于这种文件肯定使用二进制的接受方式。
我本来想直接用原生get请求,然后url=“…/static/”,然后发现这样解析其实是在解析你的index.html文件,如果全部拼接,结果就是你的首页。
原来static文件夹需要配置vue.config.js这个文件,这里说一下,static和src同级
const Cop = require('copy-webpack-plugin')
module.exports = {
configureWebpack:{
plugins: [
new Cop({
patterns:[{
from:"./static",
to:'static'
}]
})
]
}
}
npm i copy-webpack-plugin安装这个插件就好了
如果安装了axios,就很简单了
this.$axios.get(url, {responseType: "arraybuffer"})
.then( (res) => {
console.log(res.data)
}).catch((err) => {
console.log(err)
})
url这里要说一下,如果没有在vue.config.js里配置,url = ‘http://localhost:8080/static/’ + 路径,
如果配置了,url=’/api/static/’ + 路径
responseType这个是用于设置返回的文件类型的,res.data就是拿到的二进制数据,其实所有的文件都可以用arraybuffer这种方式接收,只是最后需要转化成你想要的文件格式或者数据