【问题标题】:How can I save a zip file to local storage in a win8 app using JSZip?如何使用 JSZip 将 zip 文件保存到 win8 应用程序中的本地存储?
【发布时间】:2013-08-07 14:10:15
【问题描述】:

我可以在我的代码中创建 JSZip 对象,但我无法将其保存到我的 Windows 8 应用程序的本地存储中。我能找到的示例将浏览器的 location.href 设置为触发下载,这对我来说并不是一个真正的选择。

我在下面包含了我的代码。我最终得到的 zip 文件无效,无法打开。任何帮助将不胜感激。

供参考:JSZip

        function _zipTest() {
        var dbFile = null;
        var zipData = null;
        Windows.Storage.StorageFile.getFileFromPathAsync(config.db.path)
            .then(function (file) {
                dbFile = file;
                return Windows.Storage.FileIO.readBufferAsync(file);
            })
            .then(function (buffer) {
                //Read the database file into a byte array and create a new zip file
                zipData = new Uint8Array(buffer.length);
                var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
                dataReader.readBytes(zipData);
                dataReader.close();

                var localFolder = Windows.Storage.ApplicationData.current.localFolder;
                return localFolder.createFileAsync(dbFile.displayName.concat('.zip'), Windows.Storage.CreationCollisionOption.replaceExisting)
            })
            .then(function (file) {
                //Write the zip data to the new zip file
                var zip = new JSZip();
                zip.file(dbFile.displayName, zipData);
                var content = zip.generate();

                return Windows.Storage.FileIO.writeTextAsync(file, content);
            });
    }

【问题讨论】:

    标签: windows-8 local-storage winjs jszip


    【解决方案1】:

    你可以在这些线上做点什么。此代码似乎在 temp 文件夹中生成有效的 .zip 文件。

        var zip = new JSZip();
        var storage = Windows.Storage;
        storage.StorageFile.getFileFromApplicationUriAsync(new Windows.Foundation.Uri('ms-appx:///images/logo.png')).then(function ongetfile(file)
        {
            var blob = MSApp.createFileFromStorageFile(file);
            var url = URL.createObjectURL(blob, { oneTimeOnly: true });
            return WinJS.xhr({ url: url, responseType: 'arraybuffer' });
        }).then(function onreadbuffer(req)
        {
            var b = req.response;
            zip.file('logo.png', b);
            return storage.ApplicationData.current.temporaryFolder.createFileAsync('a.zip', storage.CreationCollisionOption.replaceExisting);
        }).then(function onnewfile(out)
        {
            var content = zip.generate({ type: 'uint8array' });
            return storage.FileIO.writeBytesAsync(out, content);
        }).then(null, function onerror(error)
        {
            // TODO: error handling
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 2022-07-05
      • 2018-08-14
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 2021-11-07
      相关资源
      最近更新 更多