如何在 AsyncControl 中 MVC 下载文件?

发布于 2024-10-12 05:10:25 字数 841 浏览 3 评论 0原文

我有

public class FileController : AsyncController
{
  public ActionResult Download(FormCollection form)
  {
    FileContentResult file = new FileContentResult(Encoding.UTF8.GetBytes("10k file size"),"application/vnd.xls");
    file.FileDownloadName = "test.xls";

    return file;
  }
}

当然,

<% var form = Ajax.BeginForm(...) %>
  <input type="image" src="...gif" /> // this is my 1st attempt
  <%= Ajax.ActionLink(...) %>         // 2nd attempt
<% form.EndForm(); %>
  1. ajax形式,我尝试第一种方法(输入类型=图像)。它达到正确的行动。但客户端没有文件下载。
  2. 然后我尝试使用我真的很讨厌的 Ajax.ActionLink 。我想要漂亮的图像按钮,而不是链接文本。再次,它达到了正确的操作并且没有文件下载。但如果我在另一个窗口中打开链接,就会有文件下载!

如何使用 AsyncController 下载一个漂亮的文件

如何使 Ajax.ActionLink 看起来不错

I have

public class FileController : AsyncController
{
  public ActionResult Download(FormCollection form)
  {
    FileContentResult file = new FileContentResult(Encoding.UTF8.GetBytes("10k file size"),"application/vnd.xls");
    file.FileDownloadName = "test.xls";

    return file;
  }
}

and ofcourse, ajax form

<% var form = Ajax.BeginForm(...) %>
  <input type="image" src="...gif" /> // this is my 1st attempt
  <%= Ajax.ActionLink(...) %>         // 2nd attempt
<% form.EndForm(); %>
  1. i try first method(input type=image). it reach correct Action. but no file download in client side.
  2. Then i try to use Ajax.ActionLink which i really hate. i want nice image button, not link text. Again, it reach correct Action and no file download. But if i open link in another window, there's file download !!

Q. How to make a nice file downlaod with AsyncController

Q. How to make Ajax.ActionLink lok nice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

述情 2024-10-19 05:10:25

您无法使用 Ajax 从服务器下载文件。原因是,即使您在 success 回调中成功向服务器发出异步请求,您也将获得从服务器发送的文件内容,并且您无法在服务器上对该文件执行太多操作客户端。请记住,javascript 无法访问文件系统,因此您将无法保存它。实现此目的的方法是使用一个普通的 HTML

,它将指向 Download 操作。提交此表单后,系统将要求用户选择要保存文件的位置,然后将继续下载。此外,您不需要为此使用 AsyncController。

这是一个示例:

public class FileController : Controller
{
    [HttpPost]
    public ActionResult Download()
    {
        return File(
            Encoding.UTF8.GetBytes("10k file size"),
            "application/vnd.xls",
            "test.xls"
        );
    }
}

在您的视图中:

<% using (Html.BeginForm("download", "file", FormMethod.Post)) { %>
    <input 
        type="image" 
        src="<%: Url.Content("~/content/images/download.png") %>" 
        value="download" 
        alt="download" 
    />
<% } %>

为了使下载按钮看起来漂亮,您可以创建一个漂亮 download.png 图像它将用作表单提交按钮。

You cannot use Ajax to download files from the server. The reason for this is that even if you succeed to make the async request to the server in the success callback you will get the file contents as sent from the server and you cannot do much with this file on the client side. Remember that javascript cannot access the file system so you won't be able to save it. The way to achieve this is to have a normal HTML <form> which will point to the Download action. When this form is submitted the user will be asked to choose where he wants to save the file and the download will proceed. Also you don't need an AsyncController for this.

Here's an example:

public class FileController : Controller
{
    [HttpPost]
    public ActionResult Download()
    {
        return File(
            Encoding.UTF8.GetBytes("10k file size"),
            "application/vnd.xls",
            "test.xls"
        );
    }
}

and inside your view:

<% using (Html.BeginForm("download", "file", FormMethod.Post)) { %>
    <input 
        type="image" 
        src="<%: Url.Content("~/content/images/download.png") %>" 
        value="download" 
        alt="download" 
    />
<% } %>

And in order to make the download button look nice, you could create a nice download.png image which will be used as form submit button.

只是在用心讲痛 2024-10-19 05:10:25

我使用这个:

禁用

$('form').removeAttr("data-ajax");

以启用

$('form').attr("data-ajax", "true");

具有 3 提交 1 刷新 2 下载文件的表单
所有帖子都发布到相同的 ActionResult,但其中 2 个具有下载为 csv 或 pdf 的参数

,下载时只需禁用 ajax 提交。

I use this:

to disable

$('form').removeAttr("data-ajax");

to enable

$('form').attr("data-ajax", "true");

for a form with 3 submit 1 to refresh 2 to download file
All post to the same ActionResult but 2 of them have param to download as csv or as pdf

when download just disable ajax submission.

心的位置 2024-10-19 05:10:25

我找到办法了!!

只需创建具有常规形式的 iframe 并使用 jquery 触发即可。

$("iframe").contents().find("form").submit();

:D

ps 归功于 Firebug + Google Doc。

i found a way!!

just create iframe that has regular form and use jquery to trigger to.

$("iframe").contents().find("form").submit();

:D

ps credit to Firebug + Google Doc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文