There’s no duck code to talk about.
Upload a file
function upload() {
const el = document.createElement("input");
el.type = "file";
el.accept = ". The suffix"; // Upload file suffix
el.style.display = "none";
el.onchange = () = > {
if (el.files.length > 0) {
const fileReader = new FileReader();
fileReader.onload = () = > {
const buffer = fileReader.result;
console.log(buffer); // Upload the file content
};
fileReader.readAsArrayBuffer(el.files[0]); }}; el.click(); }Copy the code
Reading file contents
- readAsArrayBuffer()
- readAsBinaryString()
- readAsDataURL()
- readAsText()
The download file
function download() {
const buffer = "Memory for downloading files";
const projectBlob = new Blob([buffer]);
const el = document.createElement("a");
const fileName = "Download file name";
el.download = fileName + ". The suffix";
el.href = URL.createObjectURL(projectBlob);
document.body.appendChild(el);
el.click();
document.body.removeChild(el);
}
Copy the code