async handleDownload(row) { try { const res = await this.$api.downloadFileById(row.id); if (res.status === 200) { const fileUrl = window.URL.createObjectURL(new Blob([res.data])); const link = document.createElement('a');//创建a标签 link.style.display = 'none'; //将a标签隐藏 link.href = fileUrl; link.setAttribute('download','' ); link.target = '_blank'; link.click(); } else { console.log('Something went wrong'); } } catch (error) { console.error('Error occurred while downloading file:', error); } },这段代码中,responseType:'blob这行代码写哪
时间: 2024-01-09 09:05:44 浏览: 23
在你调用`this.$api.downloadFileById(row.id)`时,应该把`responseType: 'blob'`作为配置项传入。例如:
```
const res = await this.$api.downloadFileById(row.id, { responseType: 'blob' });
```
这样可以确保返回的响应数据是一个 Blob 对象。
相关问题
async getTableData(){ const res=await this.$request.get(`/links-class/page?pageNum=${this.queryInfo.pagenum}&pageSize=${this.queryInfo.pagesize}`) if(res.code==200){ this.tableData=res.data.records this.total=res.data.total } },
这段代码是一个异步函数(async function),名为 `getTableData`。在该函数内部,使用了 `await` 关键字来等待一个异步请求的结果返回。具体来说,它调用了 `this.$request.get` 方法发送了一个 GET 请求,请求的 URL 是 `/links-class/page`,并且通过 URL 的查询参数传递了 `pageNum` 和 `pageSize` 参数。
当异步请求返回结果后,使用 `res` 变量接收结果。在这段代码中,如果返回结果的 `code` 等于 200,那么将返回结果中的 `data.records` 赋值给 `this.tableData`,并将返回结果中的 `data.total` 赋值给 `this.total`。
总体来说,这段代码的作用是获取表格数据,并将获取到的数据存储到 `tableData` 和 `total` 变量中。
async onLoginTap() { //请求API let res = await this.$api.login(`${this.optionsValues[this.index]}`, { //管理员账户名 username: this.username, //管理员密码 password: this.password }); uni.setStorageSync("token", res.token); uni.setStorageSync("nickname",this.username); uni.setStorageSync("nowTable", `${this.optionsValues[this.index]}`); res = await this.$api.session(`${this.optionsValues[this.index]}`);}
这段代码是一个前端页面的登录函数,使用了异步函数和await关键字来保证请求API的结果能够获取到并赋值给res变量。其中,this.$api.login是调用了一个名为"login"的API,并传入了管理员账户名和密码。接着,将获取到的token、用户名和当前表格信息存储在本地缓存中。最后,调用了另一个名为"session"的API,并传入了当前表格信息作为参数。