服务端数据处理
public ActionResult FileDownload(int fileId)
{
var file = FileBLL.GetFileInfoById(fileId);
using (var fStream = new FileStream(file.FileLocation, FileMode.Open))
{
fStream.Seek(0, SeekOrigin.Begin);
var content = new byte[fStream.Length];
fStream.Read(content, 0, (int)fStream.Length);
fStream.Close();
return new BinaryContentResult("application/octet-stream", file.FileName, content);
}
}
public class BinaryContentResult:ActionResult
{
public string Browser { get; set; }
public string ContentType { get; set; }
public string FileName { get; set; }
public byte[] Content { get; set; }
public BinaryContentResult()
{}
文件传输
public BinaryContentResult(string contentType, string fileName, byte[] content)
{
ContentType = contentType;
FileName = fileName;
Content = content;
}
public override void ExecuteResult(ControllerContext context)
{
var browser=context.HttpContext.Request.Browser.Browser;
if (browser.ToLower().Contains("ie"))
{
var ext = FileName.Substring(FileName.LastIndexOf('.'));
var name = FileName.Remove(FileName.Length - ext.Length);
name = name.Replace(".", "%2e");
FileName = name + ext;
}
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();
}
}