1. The specific code for converting file to base64
// 图片file转base64方法(file文件,回调函数)
fileToBase64(file, callback) {
// 创建FileReader对象(不兼容IE)
let reader = new FileReader();
// 将file转为base64 (异步操作)
reader.readAsDataURL(file);
// 转换成功
reader.onload = () => {
const response = {
status: true,
data: reader.result
}
callback(response);
};
// 转换失败
reader.onerror = function () {
const response = {
status: false,
data: reader.error
}
callback(response);
};
}
// 调用方法
fileToBase64(imgFile, (res) => {
if(res.status) {
console.log('file转化成base64成功---',res.data)
} else {
console.log('file转化base64失败---',res.data)
}
})
2. Principle analysis
The principle of the method encapsulated above is mainly to use FileReader
the object to realize the conversion of the image format. The method FileReader
in the object readAsDataURL()
can read a File
or Blob
type of file and convert it into a string in base64 format. But one thing to note is that readAsDataURL()
when we read a file through a method, it is an asynchronous operation, so we need to FileReader
pass the result of the file type conversion to the method through a callback function in the onload event or onerror event of the object the caller.
And the most important point: this method is not compatible with IE.
3. FileReader
Object
The objectFileReader
can asynchronously read files on the user's computer, but only in a safe way (after obtaining the file through <input>
, DataTransfer
etc.), read the corresponding File
or Blob
type of file, and cannot read the user's storage according to the file path corresponding file.
Browser Compatibility:
① FileReader
There is only one way to create an object, and that is the constructor:
// 创建FileReader对象
let reader = new FileReader();
② Common attributes
:error
Indicates the error message returned when an error occurs while reading the file.
:readyState
Indicates the current FileReader
state of the object, with three values: 0—no data is read, 1—data is being read, 2—data reading is completed or terminated.
:result
Indicates the result of reading the file and converting the format. The format of the result depends on the reading method used.
③ Object event
:onload
This event is triggered when the read operation is completed. At this time, you can reader.result
get the result of reading the file and converting the format.
:onerror
This event is triggered when an error occurs in the read operation. At this time, you can reader.error
get the corresponding error message.
:onabort
This event is triggered when the read operation is interrupted.
④ Common methods
:readAsDataURL(file)
Read a file or Blob file, convert it to base64
a format, and store it in reader.result
.
:readAsArrayBuffer(file)
Read a file or Blob file, convert it to ArrayBuffer
a format, and store it in reader.result
.
:readAsText(file[,encoding])
Read a file or Blob file, and convert it to a string format (the default is UTF-8 format) according to the encoding type set by the second parameter, and store it in reader.result
.
:abort()
Terminates an ongoing read operation.
4. Related documents
The front end of FileReader
uses Canvas to compress pictures. Two methods.
The front end converts base64 pictures into file files.