美文网首页
js操作文件showSaveFilePicker showOpe

js操作文件showSaveFilePicker showOpe

作者: IamaStupid | 来源:发表于2022-07-18 11:28 被阅读0次

js操作文件showSaveFilePicker showOpenFilePicker

执行代码后,会弹出一个文件选择框,代码会创建一个文件,把文件保存在你选择的目录下。

const options = {
   types: [
     {
       description: "Test files",
       accept: {
         "text/plain": [".txt"],
       },
     },
   ],
 };
 
 const handle = await window.showSaveFilePicker(options);
 const writable = await handle.createWritable();
 
 await writable.write("Hello World");
 await writable.close();

这段代码也会弹出一个弹框,让你选中一个文件,让后代码会修改文件的内容

let fileHandle;
[fileHandle] = await window.showOpenFilePicker();
 
 const file = await fileHandle.getFile();
console.log(file)
 const writable = await fileHandle.createWritable();
 
 await writable.write("This is a new line");
 await writable.close();

FileSystemHandle - Web APIs | MDN (mozilla.org)

相关文章

网友评论

      本文标题:js操作文件showSaveFilePicker showOpe

      本文链接:https://www.haomeiwen.com/subject/polfirtx.html