相关疑难解决方法(0)

有没有办法在使用数据时指定建议的文件名:URI?

例如,如果您点击链接:

data:application/octet-stream;base64,SGVsbG8=

浏览器将提示您下载一个文件,该文件由超链接本身中作为base64保存的数据组成.有没有办法在标记中建议默认名称?如果没有,是否有JavaScript解决方案?

html javascript url save-as data-uri

216
推荐指数
10
解决办法
16万
查看次数

在ajax success node.js上下载文件

我正在使用node.js构建一个应用程序,需要允许用户下载.csv文件.

问题 - 当用户单击按钮时,应用程序不会将文件作为附件发送到客户端.但是,如果客户端直接转到API链接,该文件将下载.例如 - 如果用户去localhost:3000/api/exportmetric,a文件将作为附件发送到客户端.但如果该路由被作为AJAX请求命中,则没有任何反应.

用户流程:

1)用户单击一个按钮

2)App向服务器发出AJAX GET请求

3)服务器从数据库中检索数据

4)服务器将数据解析为.csv文件

5)服务器将文件发送回客户端以作为附件下载.

我的代码:

client.js

$("#export_velocity").click(function(e) {
    console.log('export to velocity hit');
    $.ajax({
        url: 'http://localhost:3001/api/exportmetric',
        type: 'GET',
        success: function(response) {
            console.log(response);
        },
        error: function(a, b, c) {
            console.log(a);
            console.log(b);
            console.log(c);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

server.js

router.get('/api/exportmetric', function(req, res) {
    console.log('export metric hit');
    var fields = ['first_name', 'last_name', 'age'];
    var fieldNames = ['First Name', 'Last Name', 'Age??'];
    var people = [
      {
        "first_name": "George",
        "last_name": "Lopez",
        "age": "31"
      }, {
        "first_name": …
Run Code Online (Sandbox Code Playgroud)

csv ajax jquery node.js

6
推荐指数
1
解决办法
5085
查看次数

标签 统计

ajax ×1

csv ×1

data-uri ×1

html ×1

javascript ×1

jquery ×1

node.js ×1

save-as ×1

url ×1