【问题标题】:chrome.filesystem creating file and rename itchrome.filesystem 创建文件并重命名
【发布时间】:2016-01-06 12:02:28
【问题描述】:

我的一些打印机有问题,我的 chrome 应用程序在文件夹中创建包含内容的文件(.xml),然后打印机打印它,但问题是 chrome 首先创建空文件,然后在其中写入内容..

喜欢这个问题的答案之一:"Premature end of file" error when Java read and writes XML data files

将您的文件写入name.xml.part,然后一旦完成/关闭到一个 重命名为 thename.xml,这使得写入更接近于原子—— 只要它确实完成了,读者就无法阅读它 仅查找“.xml”文件。

如何在 chrome 应用中做到这一点?

这是我目前的代码:

    chrome.fileSystem.getWritableEntry(printer_location, function(entry) {
        var uniqid = (Math.floor((Math.random() * 10) + 1)+Date.now()).toString();
        entry.getFile(uniqid+'.xml', {create:true}, function(entry) {
            entry.createWriter(function(writer) {
                writer.write(new Blob([xmlPrintText], {type: 'text/plain'}));
            });
        });
    });

此代码创建文件,但我需要使用.part 创建,然后在完成后重命名它而不使用 .part。

【问题讨论】:

    标签: xml google-chrome printing google-chrome-extension google-chrome-app


    【解决方案1】:

    找到解决方案:

    chrome.fileSystem.getWritableEntry(print_location, function(entry) {
        var uniqid = (Math.floor((Math.random() * 10) + 1)+Date.now()).toString();
        var fileName = uniqid+'.xml';
        entry.getFile(fileName+'.part', {create:true}, function(entry) {
            entry.createWriter(function(writer) {
                writer.write(new Blob([xmlPrintText], {type: 'text/plain'}));
                writer.onwriteend = function(e) {
                    rename(fileName+'.part', fileName);
                };
            });
        });
    });
    function rename(oldName, newName) {
        chrome.fileSystem.getWritableEntry(printer_location, function(entry) {
            entry.getFile(oldName, {}, function(fileEntry) {
                fileEntry.moveTo(entry, newName);
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2022-02-03
      • 1970-01-01
      相关资源
      最近更新 更多