简体   繁体   中英

How to read an .AppImage from Browser

I'm trying to read an appimage from the browser's drag and drop using JS and then saving it elsewhere using node.js FS module and I've tried with all of the browsers file reader options and none of them seem to work. (they all give me a file size different than the original file) https://developer.mozilla.org/en-US/docs/Web/API/FileReader

function saveMe(readFile,filename) {
  var reader = new FileReader();
  // Read file into memory as UTF-16
  reader.readAsBinaryString(readFile, "UTF-16");
  // Handle progress, success, and errors
  reader.onprogress = ()=>{};
  reader.onloadend = ()=>{
      //console.log(reader.readyState);
      if(reader.readyState==2){
      //Create File './storage/apps/'+filename, reader.result
      console.log(reader.result);
        fs.writeFile('./storage/apps/'+filename,reader.result,(err)=>{
          if(err){
            console.log("Error While reading file");
          }else{

          }
        });
      }else{

      }
  };
  reader.onerror = ()=>{console.log("Error reading file")};
}

Which method should I use?
- FileReader.readAsArrayBuffer()
- FileReader.readAsBinaryString()
- FileReader.readAsDataURL()
- FileReader.readAsText()

Read as UTF-16 and convert it to base64 when writing, also set the write options encoding to base64

function saveMe(readFile,filename) {
  var reader = new FileReader();
  // Read file into memory as UTF-16
  reader.readAsBinaryString(readFile, "UTF-16");
  // Handle progress, success, and errors
  reader.onprogress = ()=>{};
  reader.onloadend = ()=>{
      //console.log(reader.readyState);
      if(reader.readyState==2){
      //Create File './storage/apps/'+filename, reader.result
      //console.log(reader.result);
        fs.writeFile('./storage/apps/'+filename,window.btoa(reader.result),{encoding: "base64"},(err)=>{
          if(err){
            console.log("Error While reading file");
          }else{

          }
        });
      }else{

      }
  };
  reader.onerror = ()=>{console.log("Error reading file")};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM