FileReader.readAsDataURL
时间: 2023-12-19 17:02:17 浏览: 27
FileReader.readAsDataURL is a method in JavaScript that allows you to read the contents of a specified file and return its data as a base64-encoded string. It is commonly used to read image files and convert them into a format that can be easily displayed or transmitted.
To use this method, you would first create an instance of the FileReader object and then call the readAsDataURL method, passing in the file you want to read as a parameter. Here's an example:
```javascript
// Create a new FileReader object
var reader = new FileReader();
// Set up an event handler for when the file has been read
reader.onload = function(event) {
// The result property contains the data as a base64-encoded string
var dataURL = event.target.result;
console.log(dataURL);
}
// Read the contents of the file as a data URL
reader.readAsDataURL(file);
```
In the example above, "file" represents the file you want to read. Once the file is read, the onload event will be triggered, and you can access the base64-encoded data URL through the event's result property.