Created
July 5, 2021 04:50
-
-
Save wildroo/4dde1542bdcde43717368524e63dadc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Axios from "axios"; | |
import { API_URL } from "../models/constants"; | |
export const uploadImage = async (token, imageUrl) => { | |
const fileBlob = await urlToObject(imageUrl); | |
var bodyFormData = new FormData(); | |
bodyFormData.append('file', fileBlob); | |
await Axios({ | |
method: 'post', | |
url: API_URL + 'files/index.php', | |
data: bodyFormData, | |
headers: { | |
'Content-Type': 'multipart/form-data', | |
'Authorization' : token, | |
'Crossorigin' : 'true' | |
} | |
}) | |
.then(function (response) { | |
console.log(response); | |
}) | |
.catch(function (response) { | |
console.log(response); | |
}); | |
} | |
//If you need to convert an URL with a pic to file object | |
const urlToObject = async (imageUrl) => { | |
const response = await fetch(imageUrl); | |
const blob = await response.blob(); | |
const file = new File([blob], 'image.jpg', { type: blob.type }); | |
return file; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment