0

I have a controller action which returns a password protected zip file to the user when the click a link. The password is generated randomly.

If the download is successful, I'd like to then call another action method on the controller to get the password and display it on the screen. I'd be happy to do it all in one request but it doesn't feel possibly in a non horrible way.

I'm using this library to download the file jquery download library

I can't use tempdata or session state and hitting the database feels a little bit like using a hammer to crack a nut. I've thought about storing it in the response or a cookie but that feels a bit wrong too.

4

2 回答 2

0

URL 或隐藏字段应该这样做(也就是说,它们都是用户存储的,作为 POST 或 GET 请求的一部分)。问题是,如何将密码返回给客户端并同时开始下载。如果没有服务器存储,你就不能这样做,因为它们是单独的 HTTP 请求(除非你使用 cookie)。我认为这是最好的方法:

  1. 打开你的下载页面,并生成当时的密码,这样你就可以把它放在隐藏字段中(用于jquery下载库的successCallback)
  2. 调用服务器下载文件时,将密码作为 get 或 post 传递到下载 URL,并使用此密码进行压缩
  3. 下载完成后,您知道密码是什么,因此您可以重定向到另一个页面并显示它。实际上,由于您已经在页面上拥有它,您可以在使用 javascript 下载后立即显示它。或者,也许您可​​以在下载仍在进行时立即显示密码。

所以,这可能是你的架构的逆转。这可能是不可接受的(您可能希望生成安全密码并保证用户不会弄乱它们 - 如果您在下载请求中传递密码,这是可能的)。但这是没有服务器或 cookie 存储的唯一方法。

于 2013-09-27T15:03:10.317 回答
0

使用 return RedirectToAction("Action", new { id = 99 });

于 2013-09-27T15:04:02.060 回答