From c7afbc37d60c12f4393ba61b935fd5fa4d4db96b Mon Sep 17 00:00:00 2001 From: Kees Wiegel Date: Thu, 17 May 2018 15:23:32 +0200 Subject: [PATCH] Making word-export more versatile - Returning the blob since we might want to use it in our code - Making the autoSaveAs optional, sometimes we want to simply get a blob and handle saving ourselves. Use case where I needed it: I needed to create a zip folder with js-zip, and wanted to save .doc files inside the zip folder. Therefor, I needed the blobs create by word-export. I was never going to save the doc files, I wanted to save the zip file. This commit performs the necessary changes to support such a use case, and is also 100% backwards compatible. --- jquery.wordexport.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/jquery.wordexport.js b/jquery.wordexport.js index 6efb1d9..d7a60e3 100755 --- a/jquery.wordexport.js +++ b/jquery.wordexport.js @@ -1,7 +1,8 @@ if (typeof jQuery !== "undefined" && typeof saveAs !== "undefined") { (function($) { - $.fn.wordExport = function(fileName) { + $.fn.wordExport = function(fileName, autoSaveAs) { fileName = typeof fileName !== 'undefined' ? fileName : "jQuery-Word-Export"; + autoSaveAs = typeof autoSaveAs !== 'undefined' ? autoSaveAs : true; var static = { mhtml: { top: "Mime-Version: 1.0\nContent-Base: " + location.href + "\nContent-Type: Multipart/related; boundary=\"NEXT.ITEM-BOUNDARY\";type=\"text/html\"\n\n--NEXT.ITEM-BOUNDARY\nContent-Type: text/html; charset=\"utf-8\"\nContent-Location: " + location.href + "\n\n\n\n_html_", @@ -71,7 +72,10 @@ if (typeof jQuery !== "undefined" && typeof saveAs !== "undefined") { var blob = new Blob([fileContent], { type: "application/msword;charset=utf-8" }); - saveAs(blob, fileName + ".doc"); + if (autoSaveAs) { + saveAs(blob, fileName + ".doc"); + } + return blob; }; })(jQuery); } else {