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
The Drag and Drop API adds draggable elements to ...
Today, after the game was restarted, I found that...
Download the secure terminal MobaXterm_Personal F...
Table of contents Preface NULL in MySQL 2 NULL oc...
Just as the title! The commonly used font-family l...
1. CSS element hiding <br />In CSS, there ar...
Table of contents 1. How to switch 2. Dynamically...
Docker tag detailed explanation The use of the do...
If someone asked you whether running EXPLAIN on a...
<br />Some web pages may not look large but ...
Table of contents background Solution New Questio...
Of course, there are many people who hold the oppo...
The article mainly records the simple installatio...
HTML is made up of tags and attributes, which are...
Table of contents Preface How to solve Sudoku Fil...