📜  javascript 提示下载位置 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:46.993000             🧑  作者: Mango

代码示例1
// ...
const blob = new Blob(/*...*/);
// Use File System Access API
saveFileToDisk(blob, 'Some-File.txt')

async saveFileToDisk({blob, fileName}){
      try {
        const fileHandle = await self.showSaveFilePicker({
          suggestedName: fileName,
          types: [
            {
              description: "File",
              // ...
            },
          ],
        });
        const writeFile = async (fileHandle, contents) => {
          // Create a FileSystemWritableFileStream to write to.
          const writable = await fileHandle.createWritable();
          // Write the contents of the file to the stream.
          await writable.write(contents);
          // Close the file and write the contents to disk.
          await writable.close();
        };
        // write file
        writeFile(fileHandle, blob).then(() => console.log("FILE DOWNLOADED!!!"));
      } catch (error) {
        console.log(error);
      }
}