A pitfall and solution of using fileReader

A pitfall and solution of using fileReader

A pitfall about fileReader

When 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:
  • File, FileReader and Ajax file upload example analysis (php)
  • js uses FileReader to read local files or blobs
  • Use JS to operate files (FileReader reads --node's fs)
  • How to get the contents of .txt file through FileReader in JS

<<:  A brief discussion on the design and optimization of MySQL tree structure tables

>>:  Implementation steps for installing RocketMQ in docker

Recommend

Implementation of CSS equal division of parent container (perfect thirds)

The width of the parent container is fixed. In or...

Implementation of Docker private warehouse registry deployment

As more and more Docker images are used, there ne...

Nested display implementation of vue router-view

Table of contents 1. Routing Configuration 2. Vue...

A Brief Analysis of CSS Selector Grouping

Selector Grouping Suppose you want both the h2 el...

How to completely delete the MySQL 8.0 service under Linux

Before reading this article, it is best to have a...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...

JavaScript Array Detailed Summary

Table of contents 1. Array Induction 1. Split a s...

This article takes you to explore NULL in MySQL

Table of contents Preface NULL in MySQL 2 NULL oc...

MySQL database connection exception summary (worth collecting)

I found a strange problem when deploying the proj...

Basic usage of wget command under Linux

Table of contents Preface 1. Download a single fi...

Sample code for testing technology application based on Docker+Selenium Grid

Introduction to Selenium Grid Although some new f...