通过流下载可以验证身份及收费等操作,还能避免路径外漏。
如果视频观看收费也建议使用流分段下载,这样防止文件整体被copy。爱奇艺VIP会员观看视频就是这样干的,分段观看15秒左右一段
1、文件普通下载方式:直接将<a href="XXXXXXXX/HOI.mp4"/>
2、通过一般处理程序流下载:<a href="DownLoad.ashx"/> 并没有获取到文件本身的路径
DownLoad.ashx后台代码:
/// <summary>
/// DownLoad 的摘要说明
/// </summary>
public class DownLoad : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//创建文件读取对象 C:\Users\ykmy\Desktop\缓存\WebApplication2\WebApplication2\js\hdnj01.mp4
using (FileStream fileReader = new FileStream(@"C:\Users\ykmy\Desktop\缓存\WebApplication2\WebApplication2\js\hdnj01.mp4", FileMode.Open, FileAccess.Read, FileShare.Read))
{
//(filename, FileMode.Append, FileAccess.Write,FileShare.Write))
context.Response.ContentType = "application/octet-stream";
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(@"hdnj01.mp4", System.Text.Encoding.UTF8));
context.Response.AddHeader("Content-Length", fileReader.Length.ToString());
//指定文件一次读取时的字节长度
byte[] by = new byte[1024 * 128];//控制速度[128KB]
int count = 0;
while (true)
{
//将文件转换为二进制数据保存到内存中,同时返回读取字节的长度
count = fileReader.Read(by, 0, by.Length);
if (count == 0)//文件是否全部转换为二进制数据
{
break;
}
//将二进制数据转换为文件对象并保存到指定的物理路径中
context.Response.OutputStream.Write(by, 0, by.Length);
try
{
context.Response.Flush();
}
catch
{
context.Response.Close();
}
}
context.Response.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
3、直接post通过网页下载 同上(属于流下载)
web:代码
//调用
postExcelFile(
PostData,
"/AjaxServer/XXXX.ashx?Action=XXXX&r=" + Math.random()
);
// 导出excel
function postExcelFile(params, url) {
//params是post请求需要的参数,url是请求url地址
var form = document.createElement("form");
form.style.display = "none";
form.action = url;
form.method = "post";
document.body.appendChild(form);
// 动态创建input并给value赋值
for (var key in params) {
var input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = params[key];
form.appendChild(input);
}
form.submit();
form.remove();
}
后台代码
string strTime = DateTime.Now.ToString("yyyy-MM-dd_HHmmss");
string strFile = currentContext.Server.MapPath("~/temp/check" + strTime + ".xls");
NPOIHelper.Export(dt, "", strFile);
//-----下载 BEGIN------
HttpContext curContext = HttpContext.Current;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AddHeader("Content-Disposition",
"attachment; filename=" + HttpUtility.UrlEncode("XXXXXXList" + strTime + ".xls", Encoding.UTF8));
// HttpContext.Current.Response.AddHeader("Content-Length", strFile.Length.ToString());
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (MemoryStream ms = NPOIHelper.Export(dt, ""))
{
byte[] data = ms.ToArray();
HttpContext.Current.Response.BinaryWrite(data);
}
HttpContext.Current.Response.Flush();
//-----下载 END------
附目录
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Text;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.POIFS;
using NPOI.Util;
namespace BitAuto.Equipment.Framework
{
public class NPOIHelper
{
/// <summary>
/// DataTable导出到Excel文件
/// </summary>
/// <param name="dtSource">源DataTable</param>
/// <param name="strHeaderText">表头文本</param>
/// <param name="strFileName">保存位置</param>
public static void Export(DataTable dtSource, string strHeaderText, string strFileName)
{
using (MemoryStream ms = Export(dtSource, strHeaderText))
{
using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
}
}
}
public static void CommonWebExport(DataTable dtSource, string strFileName)
{
HttpContext curContext = HttpContext.Current;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
//HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.BinaryWrite(CommonExport(dtSource).GetBuffer());
HttpContext.Current.Response.End();
}
public static MemoryStream CommonExport(DataTable table)
{
MemoryStream ms = new MemoryStream();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet();
HSSFRow headerRow = sheet.CreateRow(0);
// handling header.
foreach (DataColumn column in table.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
// handling value.
int rowIndex = 1;
foreach (DataRow row in table.Rows)
{
HSSFRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in table.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}
rowIndex++;
}
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
return ms;
}
/// <summary>
/// DataTable导出到Excel的MemoryStream
/// </summary>
/// <param name="dtSource">源DataTable</param>