在浏览器中下载及打开文件的代码

    在asp.net中,下载文件和在浏览器中打开文件是经常使用的一个功能。
    其中的关键点有三个:
         1 ContentType 
2 URL
    3Content-disposition

ContentType

表示类容类型决定浏览器将以什么形式、什么编码读取这个文件。

例如myimage.gif,表示输出gif图片,application/pdf表示输入pdf文档,application/msword表示输出word文档
常见ContentType

2URL

由于需要用到某一个文件,而文件经常是中文的,这就需要处理url,否则就有可能出现乱码,导致下载打开失败。

3Content-disposition

这则控制在浏览器中下载还是直接打开文档。需要说明的是,打开的代码,可能由于浏览器的不同,也有可能不会在浏览器中打开,而出现必选在保存再打开的情况。
打开下载代码
  /// <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);

        }

代码2
 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);

        }
    }





你可能感兴趣的:(在浏览器中下载及打开文件的代码)