在Asp.Net MVC 2文件下载 [英] File download in Asp.Net MVC 2

查看:88
本文介绍了在Asp.Net MVC 2文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用文件下载在我的MVC应用程序,而不简单地用一个超链接。我打算使用图像等,并使其通过点击使用jQuery。目前,我有一个简单的只是用于测试。

I want to enable file download in my MVC application, without simply using a hyperlink. I plan to use an image or the like and make it clickable by using jQuery. At the moment I have a simple just for testing.

我发现通过诉讼的方法去下载的解释,可惜的例子仍然有actionlinks。

I found an explanation of doing the download through an action method, but unfortunately the example still had actionlinks.

现在,我可以调用下载操作方法就好了,但没有任何反应。我想我必须做的返回值的东西,但我不知道是什么或怎么样。

Now, I can call the download action method just fine, but nothing happens. I guess I have to do something with the return value, but I don't know what or how.

下面是操作方法:

    public ActionResult Download(string fileName)
    {
        string fullName = Path.Combine(GetBaseDir(), fileName);
        if (!System.IO.File.Exists(fullName))
        {
            throw new ArgumentException("Invalid file name or file does not exist!");
        }

        return new BinaryContentResult
        {
            FileName = fileName,
            ContentType = "application/octet-stream",
            Content = System.IO.File.ReadAllBytes(fullName)
        };
    }

这里的BinaryContentResult类:

Here's the BinaryContentResult class:

public class BinaryContentResult : ActionResult
{
    public BinaryContentResult()
    { }

    public string ContentType { get; set; }
    public string FileName { get; set; }
    public byte[] Content { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {

        context.HttpContext.Response.ClearContent();
        context.HttpContext.Response.ContentType = ContentType;

        context.HttpContext.Response.AddHeader("content-disposition",

                                               "attachment; filename=" + FileName);

        context.HttpContext.Response.BinaryWrite(Content);
        context.HttpContext.Response.End();
    }
}

我通过调用操作方法:

I call the action method via:

<span id="downloadLink">Download</span>

这是变成可点击通过:

which is made clickable via:

$("#downloadLink").click(function () {
    file = $(".jstree-clicked").attr("rel") + "\\" + $('.selectedRow .file').html();
    alert(file);
    $.get('/Customers/Download/', { fileName: file }, function (data) {
        //Do I need to do something here? Or where?
    });
});

注意文件名参数由操作方法,一切都正确接收,这只是没有任何反应,所以我想我需要以某种方式处理的返回值?

Note that the fileName parameter is received correctly by the action method and everything, it's just that nothing happens so I guess I need to handle the return value somehow?

推荐答案

您不想下载使用AJAX的文件,您希望浏览器下载。 $获得()将获取,但没有办法从JavaScript,在本地保存出于安全原因,浏览器需要参与。简单地重定向到下载位置,浏览器会为您处理它。

You don't want to download the file using AJAX, you want the browser to download it. $.get() will fetch it but there's no way to save locally from Javascript, for security reasons the browser needs to be involved. Simply redirect to the download location and the browser will handle it for you.

这篇关于在Asp.Net MVC 2文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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