简体   繁体   English

如何使用现代 FileApi 在本地 fs 中创建文件?

[英]How to create file in the local fs using modern FileApi?

I have this code:我有这个代码:

 document.querySelector('#myfile').onchange = function(e) { var files = this.files; window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function(fs) { let file = files[0]; let nem_file_name = file.name + '_copy'; fs.root.getFile(nem_file_name, { create: true, exclusive: true }, function(fileEntry) { fileEntry.createWriter(fileWriter => { fileWriter.write(file); }, () => alert('error 1')); }, err => alert('error 2 ' + err)); }, () => alert('error 3')); };
 <input type="file" id="myfile" ref="myfile" multiple />

I want to create a copy of my file when I select it with the input control.当我使用输入控件 select 时,我想创建我的文件的副本。 What's wrong with my code?我的代码有什么问题? I got no errors and nothing happens我没有错误,也没有任何反应

The " modern " API is called File System Access , it is still only a proposal, but is expected to supersede the previous and deprecated File System API , and is already available in Chromium browsers. 现代” API 被称为File System Access ,它仍然只是一个提议,但有望取代以前的和已弃用的文件系统 API ,并且已经在 Chromium 浏览器中可用。

To write a file using this API, you first request a FileHandle using the showSaveFilePicker() method, then you can create a writer from that handle and append data using that writer:要使用此 API 写入文件,首先使用showSaveFilePicker()方法请求 FileHandle,然后您可以使用该写入器从该句柄和 append 数据创建写入器:

onclick = async (e) => { // needs to be initiated by an user gesture
  const handle = await showSaveFilePicker(); // prompt "Save As"
  const writer = await handle.createWritable(); // request writable stream
  await writer.write( new Blob( [ "some data" ] ) ); // write the Blob directly
  writer.close(); // end writing
};

But this API is still overprotected, so it unfortunately can't be ran in cross-origin iframes like the ones used here by StackSnippet or most popular fiddling services.但是这个 API 仍然受到过度保护,所以不幸的是它不能像 StackSnippet 或最流行的摆弄服务使用的那样在跨域 iframe 中运行。 So in order to make a live demo, I had to make a plunker , that you must run in windowed mode .所以为了做一个现场演示,我必须做一个 plunker你必须在窗口模式下运行

And if you need to set the name of the file yourself, you need to request for a directory access using showDirectoryPicker() and then to get a FileHandle from that directory handle using its getFileHandle() method.如果您需要自己设置文件的名称,则需要使用showDirectoryPicker()请求目录访问,然后使用其getFileHandle()方法从该目录句柄中获取 FileHandle。 plnkr plnkr

The documentation states that this method is non-standard, only in Chrome and already deprecated:文档指出此方法是非标准的,仅在 Chrome 中并且已被弃用:

Even compared to the rest of the File and Directory Entries API, requestFileSystem() is especially non-standard;即使与文件和目录条目 API 的 rest 相比,requestFileSystem() 也特别不标准; only Chrome implements it, and all other browser makers have decided that they will not implement it.只有 Chrome 实现了它,所有其他浏览器制造商都决定不实现它。 It has even been removed from the proposed specification.它甚至已从提议的规范中删除。 Do not use this method!不要使用这种方法!

You should not use this.你不应该使用这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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