文件下载使用Ajax和iframe [英] File Download Using Ajax and iframe

查看:788
本文介绍了文件下载使用Ajax和iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一下做使用Ajax和iframe中的文件下载。 任何人都可以给我一个循序渐进的如何做到这一点还是知道的任何教程看到,因为我们已经使用AJAX在这个项目上,这似乎是做到这一点的最好办法一步的解释。

I have read about doing a file download using Ajax and iframes. Can anybody give me a step by step explanation of how to do this or know of any tutorials seeing as we are already using ajax on this project this seems like the best way to do this.

编辑:

好吧,这是我的看法code:

Okay this is my view code:

<div class="buttons">
<input type="button" value="Download File" class="button" id="downloadfile">

<script type="text/javascript">
    $(function () {
    $('#downloadfile').click(function (e) {
        $('#downloadIframe').attr('src', '@Url.Action("DownloadFile","Invoice")' + '/Report/Invoices');
    });
});
</script>

这是我的控制器:

public FileResult DownloadFile(int id)
    {

        byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Reports/Invoices/" + Table.First(x => x.ID == id).ID + ".pdf"));
        string fileName = Table.First(x => x.ID == id).ID.ToString();
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
    }

    public ActionResult Download(int id)
    {
        return AjaxResponse("Download", null);
    }

我有我使用了 jqGrid的点击一个行,然后打开 Jquery的上下文菜单中鉴于下载,然后在视图我按一下按钮,它应该在视图下执行剧本并返回 DownloadFile FileResult 的控制器,但没有任何反应,当我点击该按钮。

I have a Jquery context menu that I use to click on a row in a JQGrid and then open the view Download and then in the view I click the button and it should execute the script in the view and return DownloadFile FileResult in the controller but nothing happens when I click the button.

在哪里上的报告是该文件夹:〜/报告/发票

The folder where the reports are: ~/Reports/Invoices

推荐答案

我不知道内部框架,但我通过AJAX下载文件的方式仅仅是一个简单的书面响应流。

I don't know about iframes, but my way of downloading files through ajax is just a simple writing to the response stream.

    [HttpGet]
    [OutputCache(VaryByCustom="id")]
    public void DownloadFile(int id)
    {
        var path = GetFilePath(id);
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment; filename=xxx");
        Response.WriteFile(path);
        Response.End();
    }

这篇关于文件下载使用Ajax和iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆