this.uploadFileMixin(this.file, "/releaseReplyTaskFile").then((res) => { let fileUrl = res; console.log("imageUrl is ", fileUrl); this.fileUrl = fileUrl; this.getSgfInfo(fileUrl); }); var replyPlayer = 1; if (this.replyPlayer == "复盘白方" || this.replyPlayer == 2) { replyPlayer = 2; } var data = { replyPlayer: replyPlayer, sgfUrl: this.fileUrl, totalHead: this.totalHead, };这段代码,想先执行完this.getSgfInfo(fileUrl)再给totalHead赋值
时间: 2024-01-11 07:05:52 浏览: 5
您可以将代码改为如下形式:
```
this.uploadFileMixin(this.file, "/releaseReplyTaskFile").then((res) => {
let fileUrl = res;
console.log("imageUrl is ", fileUrl);
this.fileUrl = fileUrl;
this.getSgfInfo(fileUrl).then(() => {
var replyPlayer = 1;
if (this.replyPlayer == "复盘白方" || this.replyPlayer == 2) {
replyPlayer = 2;
}
this.totalHead = data.totalHead;
});
});
```
这样,当 `getSgfInfo` 方法执行完毕后,才会执行 `this.totalHead = data.totalHead`。请注意,`data` 变量需要提前定义。
相关问题
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这行代码写哪
在你调用`this.$api.downloadFileById(row.id)`时,应该把`responseType: 'blob'`作为配置项传入。例如:
```
const res = await this.$api.downloadFileById(row.id, { responseType: 'blob' });
```
这样可以确保返回的响应数据是一个 Blob 对象。
Angular中,如何获取p-fileUpload标签上传的文件, this.uploadedFile = event.files[0];获取的图片怎么存入项目前端../../../assets/photos的文件中,请写相关方法saveFile,且不用nodejs
在 Angular 中获取上传的文件可以通过监听 `onUpload` 事件来实现。在模板中,我们可以这样写:
```html
<p-fileUpload name="photo" (onUpload)="onFileUpload($event)"></p-fileUpload>
```
在组件中,我们需要实现 `onFileUpload` 方法:
```typescript
onFileUpload(event) {
this.uploadedFile = event.files[0];
}
```
其中,`this.uploadedFile` 变量就是我们获取到的上传文件。
接下来,我们可以将获取到的文件通过 `FileReader` 对象读取为 base64 编码的字符串,然后再进行存储。在组件中,我们可以这样实现 `saveFile` 方法:
```typescript
saveFile() {
if (this.uploadedFile) {
const reader = new FileReader();
reader.onload = (e: any) => {
const base64String = e.target.result;
const filename = this.uploadedFile.name;
const fileUrl = `../../../assets/photos/${filename}`;
localStorage.setItem(fileUrl, base64String);
console.log(`File ${filename} saved successfully.`);
};
reader.readAsDataURL(this.uploadedFile);
} else {
console.log('No file uploaded.');
}
}
```
在这个方法中,我们首先判断是否有上传文件,如果有,就使用 `FileReader` 对象将文件读取为 base64 编码的字符串。然后,我们将文件名和存储路径拼接为一个 URL(在本例中为 `../../../assets/photos/${filename}`),并使用 `localStorage` 将 base64 字符串存储到该 URL 下。最后,我们在控制台输出保存成功的信息。
注意:在本例中,我们使用的是 `localStorage` 进行文件存储,因此文件大小受到浏览器存储限制。如果需要存储较大的文件,建议使用后端服务进行存储。