Skip to content

Instantly share code, notes, and snippets.

Created July 5, 2021 04:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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) {
.catch(function (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