点击文件下载而不在浏览器中打开

download.aspx
<%@ Page language="c#" Codebehind="download.aspx.cs" AutoEventWireup="false" Inherits="ZKSTAT.Common.download" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>download</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
        
</form>
    
</body>
</HTML>
download.cs
using System;
using System.IO;
using System.Web;
using System.Web.UI;

namespace ZKSTAT.Common
{
    
/// <summary>
    
/// download 的摘要说明。
    
/// </summary>

    public class download : System.Web.UI.Page
    
{
        
private void Page_Load(object sender, EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            string path = Request.Params["download"];
            
if(path == nullreturn;
            Stream stream 
= null;           // Buffer to read 10K bytes in chunk:
            byte[] buffer = new Byte[10240];

            
// Length of the file:
            int length;

            
// Total bytes to read:
            long dataToRead;

            
// Identify the file to download including its path.
            string path2 = Request.PhysicalPath;
            path2 
= path2.Substring ( 0,path2.LastIndexOf ( "Common\\" ) );
            
string filepath  = path2 +"UploadFile/Files/" + path;
            
//filepath = Server.MapPath ( filepath );

            
// Identify the file name.
            string  filename  = Path.GetFileName(filepath);

            
try
            
{
                
// Open the file.
                stream = new FileStream(filepath, FileMode.Open, 
                                        FileAccess.Read,FileShare.Read);
                Response.Clear();

                
// Total bytes to read:
                dataToRead = stream.Length;

                
long p = 0;
                
if(Request.Headers["Range"]!=null)
                
{
                    Response.StatusCode 
= 206;
                    p 
= long.Parse( Request.Headers["Range"].Replace("bytes=","").Replace("-",""));
                }

                
if(p != 0)
                
{
                    Response.AddHeader(
"Content-Range","bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString());                    
                }

                Response.AddHeader(
"Content-Length",((long)(dataToRead-p)).ToString());
                Response.ContentType 
= "application/octet-stream";
                Response.AddHeader(
"Content-Disposition""attachment; filename=" + HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename)));

                stream.Position 
= p;
                dataToRead 
= dataToRead - p;
                
// Read the bytes.
                while (dataToRead > 0)
                
{
                    
// Verify that the client is connected.
                    if (Response.IsClientConnected) 
                    
{
                        
// Read the data in buffer.
                        length = stream.Read(buffer, 010240);

                        
// Write the data to the current output stream.
                        Response.OutputStream.Write(buffer, 0, length);

                        
// Flush the data to the HTML output.
                        Response.Flush();

                        buffer
= new Byte[10240];
                        dataToRead 
= dataToRead - length;
                    }

                    
else
                    
{
                        
//prevent infinite loop if user disconnects
                        dataToRead = -1;
                    }

                }

            }

            
catch (Exception ex) 
            
{
                
// Trap the error, if any.
                Response.Write("错误 : " + ex.Message);
            }

            
finally
            
{
                
if (stream != null
                
{
                    
//Close the file.
                    stream.Close();
                }

                Response.End();
            }

        }


        
Web 窗体设计器生成的代码
    }

}

posted @ 2006-07-06 11:44  Kangaroo  阅读(563)  评论(0编辑  收藏  举报