I do have an image url lets say http://localhost/sample.jpg
. i want to save this image url into a File
object type as my component created. how can i achieve this with native js api?
JavaScript
x
12
12
1
export default {
2
created () {
3
const imageUrl = 'http://localhost/sample.jpg'
4
const file = this.getFileFromUrl(imageUrl)
5
},
6
methods: {
7
getFileFromUrl (url) {
8
// ... what should i return?
9
}
10
}
11
}
12
Advertisement
Answer
One of the simple ways to do this is using fetch
.
JavaScript
1
8
1
let url = '...'
2
3
fetch(url)
4
.then(response => response.blob())
5
.then(blob => {
6
7
})
8
After you have blob you can convert it to file. See How to convert Blob to File in JavaScript.