原帖地址:https://www.cnblogs.com/wind-ye/articles/5171578.html
文件下载类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace pxcom
{
/// <summary>
/// 该类继承了ActionResult,通过重写ExecuteResult方法,进行文件的下载
/// </summary>
public class FileResult : ActionResult
{
private readonly string _filePath;//文件路径
private readonly string _fileName;//文件名称
public FileResult(string filePath, string fileName)
{
_filePath = filePath;
_fileName = fileName;
}
public override void ExecuteResult(ControllerContext context)
{
string fileName = _fileName;
HttpResponseBase response = context.HttpContext.Response;
if (File.Exists(_filePath))
{
FileStream fs = null;
byte[] fileBuffer = new byte[1024];//每次读取1024字节大小的数据
try
{
using (fs = File.OpenRead(_filePath))
{
long totalLength = fs.Length;
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (totalLength > 0 && response.IsClientConnected)//持续传输文件
{
int length = fs.Read(fileBuffer, 0, fileBuffer.Length);//每次读取1024个字节长度的内容
fs.Flush();
response.OutputStream.Write(fileBuffer, 0, length);//写入到响应的输出流
response.Flush();//刷新响应
totalLength = totalLength - length;
}
response.Close();//文件传输完毕,关闭相应流
}
}
catch (Exception ex)
{
response.Write(ex.Message);
}
finally
{
if (fs != null)
fs.Close();//最后记得关闭文件流
}
}
}
}
}
控制器:
public ActionResult GetFile(string url)
{
string path = pxcom.Util.GetKejianPath() + url;
var filename = Path.GetFileName(path);
return new pxcom.FileResult(path, filename);
}
地址栏访问:http://localhost:30324/test/getfile?url=/131575214624030721.zip
webclient下载:
public static void Down(string url,string saveFileName)
{
string svr = System.Configuration.ConfigurationManager.AppSettings["svraddress"];
url = svr + "/" + url;
new WebClient().DownloadFile(url, saveFileName);
}