Little and Mickle

All things are difficult before they are easy.

导航

Asp.Net下载文件到客户端

        /// <summary>
        
/// To download a file using Response.WriteFile(string filename)
        
/// </summary>
        
/// <param name="path"></param>

        public static void Download(string path)
        
{
            
try
            
{
                
if(path!=null)
                
{
                    FileInfo fi 
= new FileInfo(path);

                    HttpContext.Current.Response.Clear();

                    
// //当要下载的文件名是中文时,需加上HttpUtility.UrlEncode
                    HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(fi.Name));
                    HttpContext.Current.Response.AddHeader(
"Content-Length",fi.Length.ToString());
                    HttpContext.Current.Response.ContentType
="application/octet-stream";
                    HttpContext.Current.Response.WriteFile(fi.FullName);
                    HttpContext.Current.Response.End();
                }

                
else
                
{
                    CommonFunction.MessageBox(
"File Does not exist!");
                }

            }

            
catch(Exception _ex)
            
{
                
throw _ex;
            }

        }

posted on 2005-08-11 14:40  davidullua  阅读(884)  评论(0)    收藏  举报