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 :: vue.js slots 
Javascript :: js order string 
Javascript :: jquery google 
Javascript :: is object js 
Javascript :: javascript bubble sort 
Javascript :: js get hostname from url 
Javascript :: date time js 
Javascript :: node exporter service 
Javascript :: open cypress window 
Javascript :: how to get unique values from array in javascript without duplicate value 
Javascript :: how to remove space in input field html 
Javascript :: reset select form jquery 
Javascript :: express uncaughtException 
Javascript :: moment js check if date is greater than 
Javascript :: javascript random 
Javascript :: how to store array of object in local storage 
Javascript :: generate html with javascript 
Javascript :: react native onChangeText resize the background image 
Javascript :: picker change event react native 
Javascript :: how to convert node list to array in javascript 
Javascript :: Count frequency of array elements js 
Javascript :: scrapy javascript 
Javascript :: input type search clear event 
Javascript :: javascript check image src 
Javascript :: regular expression to remove underscore from a string javascript 
Javascript :: settimeout in javascript 
Javascript :: jquery fadeout and remove 
Javascript :: get last two digits of year javascript 
Javascript :: how to make apk of react native app 
Javascript :: ajax syntax in javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =