简体   繁体   English

下载文件不适用于iframe

[英]Download file not working with iframe

I'm making an express application, and am having trouble exporting a file when the user clicks "download". 我正在制作一个快速申请,当用户单击“下载”时无法导出文件。

My approach as to make an ajax request to the server with the content, and then on the server, create the file and send back the file path. 我的方法是向服务器发出含内容的ajax请求,然后在服务器上创建文件并发送回文件路径。 From there, I adjust the src attribute of an iframe element with the file path. 从那里,我用文件路径调整iframe元素的src属性。

But, nothing is happening on Chrome (or any browser, actually). 但是,Chrome(实际上没有任何浏览器)上没有任何反应。 If I look at the network tab of the inspector, it shows that it received the file OK (with a 200 response code), and the content is there. 如果查看检查器的“ 网络”选项卡,则表明它已收到OK(带有200响应代码)文件,并且内容在那里。 Also, the iframe has the correct src . 另外, iframe具有正确的src I'd like a download dialog to popup, but can't seem to get it. 我想弹出一个下载对话框,但似乎无法获取。

Any ideas? 有任何想法吗? Thank you! 谢谢!


Example Code : 示例代码

app.js (server) app.js (服务器)

app.post('/create-html', function(req, res) {

    // explicitly set the headers on the server
    res.header('Content-Type', 'application/octet-stream');
    res.header('Content-Disposition', 'attachment; filename="test.html"')

    var html      = req.body.content
       , name      = "test.html"
       , filepath  = (__dirname + "/public/files/" + name)
       , json_resp = {
            data: ''
          , err: false
         };

    fs.writeFile( filepath, html, 'utf8', function(err, data) {
        // write the file and res.send the json_resp, so we can
        // access the filename on the client
    })
})

script.js (client) script.js (客户端)

$("#export-html").click( function(e) {
    e.preventDefault();
    var html = $("#html-wrapper").html(); // the html of the file we want to make

    $.ajax({
          method: "POST"
        , url: "/create-html"
        , headers: {
            "Content-Disposition": 'attachment; filename="test.html"'
          }
        , type: "JSON"
        , success: function(resp) {
              var iframe = document.getElementById("iframe")
                , fp_prefix = "/files/"
                , fp = fp_prefix + resp.data; // ex: "/files/test.html" - the path where the file can be accessed

              iframe.src = fp
          }
    })

})

I'm getting 200 s when POSTing to the server ( /create-html ), and when the server is sending the data back ( test.html ). POST到服务器( /create-html )以及服务器向后发送数据( test.html )时,我得到200 s。 The content of the test.html file shows up in the web inspector, and going to /files/test.html shows the page. test.html文件的内容显示在Web检查器中,转到/files/test.html显示该页面。 But, I can't get it to download the HTML . 但是, 我无法下载HTML

Any ideas? 有任何想法吗?

with ExpressJS you use this: 与ExpressJS一起使用:

apt.get('/create-html', function (req, res) {
  var name       = "test.html"
      , filepath = (__dirname + "/public/files/" + name);

  res.download(filepath, name);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM