Triggering a file download without any server request

前端 未结 4 1316
粉色の甜心
粉色の甜心 2020-12-06 05:43

Greetings,

I\'m working on a JS-based application that does some complex work and logs some information (actually, up to hundreds of lines) on a

4条回答
  •  悲&欢浪女
    2020-12-06 06:27

    I have been down this road, and I also wanted to do it purely on the client-side. But, it is impossible. The only way you can trigger the save dialog without any fuss is by making a HTTP POST request, and having the server respond with a content-disposition header.

    I have your desired functionality in the code snippets on my dusty old blog. I have a form with one hidden field pointed to a custom HTTP handler. Javascript grabs the inner text of the code block, puts it in the hidden field, and submits the form. Server responds with the entire body of the request, along with the required headers. No refresh, you click it and you get a save dialog. Works great!

提交回复
热议问题