在Bootstrap模式确认按钮上单击,创建并下载文件jquery或敲除js [英] On Bootstrap modal confirm button click ,create and download file jquery or knockout js

查看:317
本文介绍了在Bootstrap模式确认按钮上单击,创建并下载文件jquery或敲除js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个引导程序模态(bootbox),对其进行确认操作,我想创建一个包含一些数字的文件,然后将其下载为文本文件.

I have a bootstrap modal( bootbox ), on which confirm action, i want to create a file which contains some numbers and download the file as a text file.

我已经介绍了创建内存中的文件供用户下载,而不是通过服务器下载,但对我而言却无济于事.

I have already referred Create a file in memory for user to download, not through server but its not helpful in my case.

这是代码:

bootbox.confirm("Item not found! Do you want to download the text file?", (yes) => {
    var items = response.itemNumbers;
    this.href = "data:text/plain;charset=UTF-8," + encodeURIComponent(items);
});

我也尝试过这个:

bootbox.confirm("item not found! Do you want to download the text file?", (yes) => {

    var itemNumbers = response.itemNumbers;
    var textFile = null;

    var data = new Blob([itemNumbers], { type: 'text/plain' });

    // deleting old text file
    if (textFile !== null) {
        window.URL.revokeObjectURL(textFile);
    }

    textFile = window.URL.createObjectURL(data);

    var link = $(textFile).attr("href");

    document.location.href = link;

});

当我运行此代码时,第一个代码运行并且什么也没有发生.第二个给出有关Blob转换的错误:语法错误,无法识别的表达式:blob:http%3A//localhost%3A51876/028f2122-1646-4b2e-ae2c-2a1b588fdd8d

EDIT : When I run this, first code runs and nothing happens. Second gives error about Blob conversion : Syntax error, unrecognized expression: blob:http%3A//localhost%3A51876/028f2122-1646-4b2e-ae2c-2a1b588fdd8d

推荐答案

知道了!谢谢@kb.为帮助.您提供的链接提供了答案.只需在此处发布供将来的访问者使用:下面的代码将创建一个包含"abcd,wxyz"的文件,并提示下载:

Got it! thanks @kb. for helping. The link you provided had the answer. Just posting here for future visitors: below code would create a file which contains "abcd,wxyz" and prompt for download :

    var items = "abcd,wxyz";
    var json = JSON.stringify(items);
    var blob = new Blob([json], {type: "octet/stream"});
    var url  = window.URL.createObjectURL(blob);
    window.location.assign(url);

reference:没有链接的JavaScript Blob文件名

reference : JavaScript blob filename without link

这篇关于在Bootstrap模式确认按钮上单击,创建并下载文件jquery或敲除js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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