簡體   English   中英

文件下載后如何重定向頁面?

[英]How to redirect a page after file download?

我想在下載文件后重定向頁面。 但文件只下載不重定向頁面

Document doc = new Document();
doc.Save("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment);
return RedirectToAction("Home", "Home");

可以通過返回多部分/混合響應來實現這一點,其中第一部分是要下載的文件,第二部分是一個META http-equiv="refresh"標簽,帶有要重定向到的 url。

此解決方案適用於 Mozilla Firefox(在 42.0 版上測試),不確定其他瀏覽器。

這是一個未完善的示例,說明如何在 Ruby on Rails 上執行此操作:

_multipart_response_boundary = '---------------------------8142823132435752456881845357'

_multipart_body = '_multipart_response_boundary' + "\r\n"

_multipart_body << 'Content-Disposition: attachment; filename="foo_bar.pdf"' + "\r\n"
_multipart_body << 'Content-Type: application/pdf' + "\r\n"
_multipart_body << "\r\n" + 'file contents' + "\r\n"

_multipart_body << '_multipart_response_boundary' + "\r\n"

_multipart_body << "\r\n" + '<html><head><META http-equiv="refresh" content="0;URL=https://www.example.com"></head><body></body></html>' + "\r\n"

_multipart_body << "\r\n" + '_multipart_response_boundary' + "--\r\n"

render(:content_type => ('multipart/mixed; boundary=' + _multipart_response_boundary), :text => _multipart_body)

我會考慮在 ajax 中提出您的下載請求,並將您的方向操作放在成功塊中。 一個控制器不能有兩個響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM