I have this upload function. It works well, except that it uploads the files twice.
startUpload(event: HTMLInputEvent) {
console.log(event) // logs once
this.tasks$ = from([Array.from(event.target.files)]).pipe(
map(files => files.map((file, index) => {
console.log(file) // logs twice
const path = `test/${index}_${file.name}`
const customMetadata = { app: 'Angular!' }
return this.afstorage.upload(path, file, { customMetadata });
})
)
)
this.snapshots$ = this.tasks$.pipe(
map(files =>
files.map(file =>
file.snapshotChanges(),
),
)
)
this.progresses$ = this.tasks$.pipe(
map(files =>
files.map(file =>
file.percentageChanges()
),
)
)
}
How do I prevent it from uploading more than once?
I think the problem is here
files.map((file, index) => {
console.log(file) // logs twice
const path = `test/${index}_${file.name}`
const customMetadata = { app: 'Angular!' }
return this.afstorage.upload(path, file, { customMetadata });
})
The arrow function provided as an argument to .map
will be called once for each file in the files
array.
So my guess is that this is some kind of file uploader, and you've specified 2 files to be uploaded, or possibly the same file twice.
In response to your comment, if you only want to call this once, eg with the first file, you could replace the code above with
const index = 0;
const file = files[0];
console.log(file) // logs twice
const path = `test/${index}_${file.name}`
const customMetadata = { app: 'Angular!' }
return this.afstorage.upload(path, file, { customMetadata });
This will only upload the first file, but skipping the rest of the files doesn't seem like a good idea.
I think there's a problem with your question. You've said you only want this code to execute once, but if multiple files are chosen, how in that case will the other files be uploaded?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.