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

How to use HTML 5 drag and drop API in Vue

The Drag and Drop API adds draggable elements to ...

A brief analysis of the game kimono memo problem

Today, after the game was restarted, I found that...

Example of how to configure nginx in centos server

Download the secure terminal MobaXterm_Personal F...

This article takes you to explore NULL in MySQL

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

Do you know how to optimize loading web fonts?

Just as the title! The commonly used font-family l...

CSS element hiding principle and display:none and visibility:hidden

1. CSS element hiding <br />In CSS, there ar...

Several ways to switch between Vue Tab and cache pages

Table of contents 1. How to switch 2. Dynamically...

Detailed explanation of the use of docker tag and docker push

Docker tag detailed explanation The use of the do...

Why the explain command may modify MySQL data

If someone asked you whether running EXPLAIN on a...

How to reduce the memory and CPU usage of web pages

<br />Some web pages may not look large but ...

Implementation of element shuttle frame performance optimization

Table of contents background Solution New Questio...

960 Grid System Basic Principles and Usage

Of course, there are many people who hold the oppo...

Getting Started: A brief introduction to HTML's basic tags and attributes

HTML is made up of tags and attributes, which are...

The complete implementation process of Sudoku using JavaScript

Table of contents Preface How to solve Sudoku Fil...