[.NET] .NET下载文件

1. 客户端

发送Get请求某个Action,将filePath作为查询参数传递
例如:

location.href='/Content/DownloadFile?filePath='+filePath;

或者fileName

2. 服务器端

public ActionResult DownloadFile(string filePath)
{
    FileInfo info = new FileInfo(filePath);
    long fileSize = info.Length;

    Response.Clear();
    Response.ContentType = "application/x-zip-compressed";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath)));
    Response.AddHeader("Content-Length", fileSize.ToString());
    Response.TransmitFile(filePath, 0, fileSize);
    Response.Flush();

    return null;
}

注:
在Response Header中加入的“filename=”,用来指定下载到客户端的默认文件名。

你可能感兴趣的:([.NET] .NET下载文件)