Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

image url to file js

const url = "https://cdn.shopify.com/s/files/1/0234/8017/2591/products/young-man-in-bright-fashion_925x_f7029e2b-80f0-4a40-a87b-834b9a283c39.jpg"
const fileName = url.substring(url.lastIndexOf('/')+1)

fetch(url)
   .then(response => response.blob())
   .then(blob => new File([blob], `${fileName}`, {
   		type: blob.type
	}))
   .then(file => {
   		setFileData(file);
   		setPreview(URL.createObjectURL(file));
	})
Comment

convert data uri to image file javascript

this will convert a dataURI to a Blob:

function dataURItoBlob(dataURI) {
    // convert base64/URLEncoded data component to raw binary data held in a string
    var byteString;
    if (dataURI.split(',')[0].indexOf('base64') >= 0)
        byteString = atob(dataURI.split(',')[1]);
    else
        byteString = unescape(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

    // write the bytes of the string to a typed array
    var ia = new Uint8Array(byteString.length);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    return new Blob([ia], {type:mimeString});
}
From there, appending the data to a form such that it will be uploaded as a file is easy:

var dataURL = canvas.toDataURL('image/jpeg', 0.5);
var blob = dataURItoBlob(dataURL);
var fd = new FormData(document.forms[0]);
fd.append("canvasImage", blob);
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular remove item from localstorage 
Javascript :: mandelbrot set javascript 
Javascript :: es6 foreach 
Javascript :: jquery if attribute 
Javascript :: border bootstrap 
Javascript :: Send Data Using Fetch With Then Syntax 
Javascript :: js push params to url 
Javascript :: stripe npm 
Javascript :: getype js 
Javascript :: smallest common multiple javascript 
Javascript :: slice eliminar el ultimo caracter 
Javascript :: how to use hover functionality using Jquery 
Javascript :: how to take an element out of an array in javascript 
Javascript :: math.round js 1 decimal 
Javascript :: javascript separate words by capital letter 
Javascript :: javascript convert to two decimal places 
Javascript :: debounce in react native hooks 
Javascript :: react native flatlist pull to refresh 
Javascript :: rectbutton disable 
Javascript :: save in json file js 
Javascript :: set radio button checked jquery 
Javascript :: delete package-lock.json command 
Javascript :: Javascript - check if div contains a word? - Stack Overflow 
Javascript :: laravel ajax delete 
Javascript :: get uploaded file name in js 
Javascript :: javascript search in object array 
Javascript :: javascript lowercase string except first letter of every word 
Javascript :: this.$router.push nuxt 
Javascript :: jquery trigger click other element 
Javascript :: random hexadecimal character js 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =