/// <summary> /// 在浏览器中打开文件,由于浏览器的不同,也有可能不会打开文件,而是变成了必须保存后才能打开 /// </summary> /// <param name="filpath"></param> /// <param name="filename"></param> public static void OpenFile(string filpath, string filename) { long fileSize = 0; byte[] fileBuffer; HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); HttpContext.Current.Response.Expires = 0; HttpContext.Current.Response.CacheControl = "no-cache"; HttpContext.Current.Response.AppendHeader("Pragma", "No-Cache"); HttpContext.Current.Response.ContentType = "application/pdf;";//输出类型 HttpContext.Current.Response.Charset = "charset=utf-8"; HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=\"" + UrlEncode(filename) + "\";");//在浏览器中打开文档,对文件名进行处理 using (FileStream fileStream = new FileStream(filpath, FileMode.Open, FileAccess.Read)) { fileSize = fileStream.Length; fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); fileStream.Close(); } HttpContext.Current.Response.BinaryWrite(fileBuffer); HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());//文件大小 HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } /// <summary> /// 在浏览器中下载文件 /// </summary> /// <param name="filpath"></param> /// <param name="filename"></param> public static void DownFile(string filpath, string filename) { long fileSize = 0; byte[] fileBuffer; HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); HttpContext.Current.Response.Expires = 0; HttpContext.Current.Response.CacheControl = "no-cache"; HttpContext.Current.Response.AppendHeader("Pragma", "No-Cache"); HttpContext.Current.Response.ContentType = "application/pdf;";//输出类型 HttpContext.Current.Response.Charset = "charset=utf-8";//编码 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + UrlEncode(filename) + "\";");//在浏览器中打开文档,对文件名进行处理 using (FileStream fileStream = new FileStream(filpath, FileMode.Open, FileAccess.Read)) { fileSize = fileStream.Length; fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); fileStream.Close(); } HttpContext.Current.Response.BinaryWrite(fileBuffer); HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());//文件大小 HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } private static string UrlEncode(string filename) { return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8); }
public class OpenDownFile { /// <summary> /// 在浏览器中打开文件,由于浏览器的不同,也有可能不会打开文件,而是变成了必须保存后才能打开 /// </summary> /// <param name="filpath"></param> /// <param name="filename"></param> public static void OpenFile(string filpath, string filename,string filetype) { long fileSize = 0; byte[] fileBuffer; HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); HttpContext.Current.Response.Expires = 0; HttpContext.Current.Response.CacheControl = "no-cache"; HttpContext.Current.Response.AppendHeader("Pragma", "No-Cache"); HttpContext.Current.Response.ContentType = filetype;//输出类型 HttpContext.Current.Response.Charset = "charset=utf-8"; HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=\"" + UrlEncode(filename) + "\";");//在浏览器中打开文档,对文件名进行处理 using (FileStream fileStream = new FileStream(filpath, FileMode.Open, FileAccess.Read)) { fileSize = fileStream.Length; fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); fileStream.Close(); } HttpContext.Current.Response.BinaryWrite(fileBuffer); HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());//文件大小 HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } /// <summary> /// 在浏览器中下载文件 /// </summary> /// <param name="filpath"></param> /// <param name="filename"></param> public static void DownFile(string filpath, string filename,string filetype) { long fileSize = 0; byte[] fileBuffer; HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); HttpContext.Current.Response.Expires = 0; HttpContext.Current.Response.CacheControl = "no-cache"; HttpContext.Current.Response.AppendHeader("Pragma", "No-Cache"); HttpContext.Current.Response.ContentType =filetype;//输出类型 HttpContext.Current.Response.Charset = "charset=utf-8";//编码 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + UrlEncode(filename) + "\";");//在浏览器中打开文档,对文件名进行处理 using (FileStream fileStream = new FileStream(filpath, FileMode.Open, FileAccess.Read)) { fileSize = fileStream.Length; fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); fileStream.Close(); } HttpContext.Current.Response.BinaryWrite(fileBuffer); HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());//文件大小 HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } private static string UrlEncode(string filename) { return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8); } }