在FileSaver.js中保存文件后关闭窗口 [英] close window after file save in FileSaver.js

查看:102
本文介绍了在FileSaver.js中保存文件后关闭窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FileSaver.js 文件将画布保存到图像。下载后我必须关闭窗口。有什么方法可以实现?

I am using the FileSaver.js file to save my canvas to image. I have to close the window after download. Is there any way to achieve this?

这是我尝试过的代码,

canvas.toBlob(function (blob) {
    saveAs(blob, "RQGantt.png");
    window.close();
});

使用小尺寸图像效果很好。但是当我有大尺寸的图像时,我的窗口在开始下载之前就关闭了。

It's works fine with small size of image. But my window closed before it's start downloading when i have a large size of image.

在客户端有什么方法吗?

Is there any way to do this in client side?

推荐答案

以下是他们为 https://github.com/eligrey/FileSaver.js/issues/52 问题。


不幸的是,JS无法确定文件何时完成保存,因此
用户将不得不关闭新标签页

Unfortunately, JS can't tell when the file has finished saving so the user will have to close new tabs it may make for content being saved.

所以,我做了以下解决方法

So, I have made a following workaround

window.onbeforeunload = function () {
    //For IE
    if (HTMLCanvasElement.prototype.msToBlob && window["ganttBlob"])
        saveAs(ganttBlob, "imageName.png");
}

canvas.toBlob(function (blob) {
    window.ganttBlob = blob;
    // For Non IE Browser
    if (!canvas.msToBlob) {
        saveAs(blob, "imageName.png");
        setTimeout(function () {
            window.close();
        }, 500);
    }
    else //For IE
        window.close();
});

因此,现在即使窗口已关闭,也会出现下载提示选项。

So, now even the window has been closed, download prompt option will comes.

这篇关于在FileSaver.js中保存文件后关闭窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆