2017-08-05 249 views
3

我有一個用HTML/Javascript編寫的UWP應用程序,並且無法保存由JSZip製作的zip文件。具體寫到磁盤是我掛在的地方。UWP將zip文件保存到磁盤

我在Microsoft文檔中看到有WriteBufferAsync,WriteBytesAsync,WriteLinesAsync和WriteTextAsync。林不知道哪一個我需要這個。 JSZip也可以生成不同的類型,比如base64,binarystring,uint8array,arraybuffer和blob。我只是不知道什麼組合我需要寫這個zip文件到用戶磁盤。

下面是我的代碼:

savePNGButton.addEventListener('click', function (e) { 
    var zip = new JSZip(); 
    if (WatermarkText === ""){ 
    ZipFolder = zip.folder("ImageFolder"); 
    } else { 
    ZipFolder = zip.folder(WatermarkText); 
    } 
    $(".WatermarkPhoto").each(function(index) { 
    imgsrc = this.src; 
    var DataURL = imgsrc.replace('data:image/png;base64,', ''); 
    ZipFolder.file(WatermarkText + index + ".png", DataURL, { base64: true }); 

    }); 
    zip.generateAsync({ type:"blob"}) 
     .then(function (content) { 
      console.log(content); 
      var savePicker = new Windows.Storage.Pickers.FileSavePicker(); 
      savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary; 
      savePicker.fileTypeChoices.insert("ZIP archive", [".zip"]); 
      savePicker.suggestedFileName = WatermarkText+".zip"; 
      savePicker.pickSaveFileAsync().then(function (file) { 
       if (file) { 
        Windows.Storage.CachedFileManager.deferUpdates(file); 
        Windows.Storage.FileIO.writeTextAsync(file, content).done(function() { 
         Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) { 
          if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) { 
           console.log("File " + file.name + " was saved."); 
          } else { 
           console.log("File " + file.name + " couldn't be saved."); 
          } 
         }); 
        }); 
       } else { 
        console.log("Operation cancelled."); 
       } 
      }); 
     }); 
}); 
+0

_「我只是不確定我需要將這個zip文件寫入用戶磁盤的組合。」_該要求僅適用於使用* indows 10操作系統的計算機,是的? – guest271314

+0

是的。這只是我試圖進入Windows應用程序商店的Windows 10應用程序。 –

回答

0

如果有人遇到這個問題,我發現這個鏈接發給我在正確的方向https://blog.appliedis.com/2013/09/18/zipping-and-unzipping-files-in-a-winjs-application/我從JSZip從窗戶FileIO專注類文件流和WriteBytesAsync一起使用uint8array 。以下是我用於壓縮和呈現文件保存對話框的最後一段代碼。

savePNGButton.addEventListener('click', function (e) { 
    var zip = new JSZip(); 
    if (WatermarkText === ""){ 
    ZipFolder = zip.folder("Images"); 
    } else { 
    ZipFolder = zip.folder(WatermarkText); 
    } 
    $(".WatermarkPhoto").each(function(index) { 
    imgsrc = this.src; 
    var DataURL = imgsrc.replace('data:image/png;base64,', ''); 
    ZipFolder.file(WatermarkText + index + ".png", DataURL, { base64: true }); 

    }); 

    zip.generateAsync({ type: "uint8array", streamFiles:"true"}) 
     .then(function (content) { 
      console.log(content); 
       var savePicker = new Windows.Storage.Pickers.FileSavePicker(); 
       savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary; 
       savePicker.fileTypeChoices.insert("ZIP archive", [".zip"]); 
       savePicker.suggestedFileName = WatermarkText + ".zip"; 
       savePicker.pickSaveFileAsync().then(function (file) { 
        if (file) { 
         Windows.Storage.CachedFileManager.deferUpdates(file); 
         Windows.Storage.FileIO.writeBytesAsync(file, content).done(function() { 
          Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) { 
           if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) { 
            console.log("File " + file.name + " was saved."); 
           } else { 
            console.log("File " + file.name + " couldn't be saved."); 
           } 
          }); 
         }); 
        } else { 
         console.log("Operation cancelled."); 
        } 
       }); 
     }); 
});