A pitfall about fileReaderWhen using fileReader for image browsing, Using base64 format var file = this.$refs.resource.files[0] var reader = new FileReader(); // Read the file as DataURL: reader.readAsDataURL(file); //The callback js after reading is multi-threaded reader.onloadend=(e)=>{ } Javascript itself is single-threaded and has no asynchronous features. Since Javascript is used in browsers, and the browser itself is a typical GUI worker thread, GUI worker threads are implemented as event processing in most systems to avoid blocking interactions, thus giving rise to the asynchronous gene of Javascript. Everything that happened afterwards originated from this. That's right, network, files. . . . All are implemented based on browser interfaces If the same file is read twice, the loading completion event will not be executed. FileReader's pitfalls on iOS (image to base64)I'm working on a project recently. Uploading pictures is a very old function. Then I want to compress the picture and upload it directly to base64... The code I used at the beginning var fileUpload = function(obj, callback){ //Check if the browser supports the FileReader object if (typeof FileReader == "undefined") { alert("Your browser does not support FileReader object!"); } var file = obj; //Judge whether the type is a picture if (!/image\/\w+/.test(file.type)){ alert("Please make sure the file is of image type"); return false; } var reader = new FileReader(); reader.onload = function (e) { var img = new Image, width = 95, //Image resize width quality = 0.2, //Image quality canvas = document.createElement('canvas'), drawer = canvas.getContext("2d"); img.src = this.result; var scale = parseInt(img.height / img.width); img.width=width; img.height = width * scale; canvas.width = img.width; canvas.height = img.height; drawer.drawImage(testimgId, 0, 0, canvas.width, canvas.height); var base64 = canvas.toDataURL('image/jpeg', 0.2); console.log(base64); var image_base64 = img.src.replace("data:image/png;base64,",""); image_base64=encodeURIComponent(image_base64); alert("19") //Call callback callback&&callback(img.src); } reader.readAsDataURL(file); } The above code works fine on PC and Android, but it returns a fixed string of base64 encoding on iOS, no matter what image you upload. Then change... Change again..... View various documents.. Continue to change... This is how it was finally resolved function getBase64(fileObj){ var file = fileObj, cvs = document.getElementById("canvas"), ctx = cvs.getContext("2d"); if(file){ var url = window.URL.createObjectURL(file); //PS: Not compatible with IE var img = new Image(); img.src = url; img.onload = function(){ ctx.clearRect(0,0,cvs.width,cvs.height); cvs.width = img.width; cvs.height = img.height; ctx.drawImage(img,0,0,img.width,img.height); var base64 = cvs.toDataURL("image/png"); callback(base64); alert("20") } } } This is the point of sharing... The reason is that FileReader objects are not supported on iOS! The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: A brief discussion on the design and optimization of MySQL tree structure tables
>>: Implementation steps for installing RocketMQ in docker
When a user registers, they will click on a label...
The width of the parent container is fixed. In or...
As more and more Docker images are used, there ne...
Table of contents 1. Routing Configuration 2. Vue...
Selector Grouping Suppose you want both the h2 el...
Before reading this article, it is best to have a...
Table of contents cause reason Introduction to NP...
1. Install xshell6 2. Create a server connection ...
Conclusion: In a multithreaded environment, if on...
Table of contents 1. Array Induction 1. Split a s...
Preface: Front-end: jq+h5 to achieve the nine-gri...
Table of contents Preface NULL in MySQL 2 NULL oc...
I found a strange problem when deploying the proj...
Table of contents Preface 1. Download a single fi...
Introduction to Selenium Grid Although some new f...