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

download.aspx
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="download.aspx.cs" AutoEventWireup="false" Inherits="ZKSTAT.Common.download"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > download </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif
download.cs
None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
None.gif
namespace  ZKSTAT.Common
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// download 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class download : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            string path = Request.Params["download"];
InBlock.gif            
if(path == nullreturn;
InBlock.gif            Stream stream 
= null;           // Buffer to read 10K bytes in chunk:
InBlock.gif
            byte[] buffer = new Byte[10240];
InBlock.gif
InBlock.gif            
// Length of the file:
InBlock.gif
            int length;
InBlock.gif
InBlock.gif            
// Total bytes to read:
InBlock.gif
            long dataToRead;
InBlock.gif
InBlock.gif            
// Identify the file to download including its path.
InBlock.gif
            string path2 = Request.PhysicalPath;
InBlock.gif            path2 
= path2.Substring ( 0,path2.LastIndexOf ( "Common\\" ) );
InBlock.gif            
string filepath  = path2 +"UploadFile/Files/" + path;
InBlock.gif            
//filepath = Server.MapPath ( filepath );
InBlock.gif
InBlock.gif            
// Identify the file name.
InBlock.gif
            string  filename  = Path.GetFileName(filepath);
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Open the file.
InBlock.gif
                stream = new FileStream(filepath, FileMode.Open, 
InBlock.gif                                        FileAccess.Read,FileShare.Read);
InBlock.gif                Response.Clear();
InBlock.gif
InBlock.gif                
// Total bytes to read:
InBlock.gif
                dataToRead = stream.Length;
InBlock.gif
InBlock.gif                
long p = 0;
InBlock.gif                
if(Request.Headers["Range"]!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Response.StatusCode 
= 206;
InBlock.gif                    p 
= long.Parse( Request.Headers["Range"].Replace("bytes=","").Replace("-",""));
ExpandedSubBlockEnd.gif                }

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

InBlock.gif                Response.AddHeader(
"Content-Length",((long)(dataToRead-p)).ToString());
InBlock.gif                Response.ContentType 
= "application/octet-stream";
InBlock.gif                Response.AddHeader(
"Content-Disposition""attachment; filename=" + HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename)));
InBlock.gif
InBlock.gif                stream.Position 
= p;
InBlock.gif                dataToRead 
= dataToRead - p;
InBlock.gif                
// Read the bytes.
InBlock.gif
                while (dataToRead > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// Verify that the client is connected.
InBlock.gif
                    if (Response.IsClientConnected) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
// Read the data in buffer.
InBlock.gif
                        length = stream.Read(buffer, 010240);
InBlock.gif
InBlock.gif                        
// Write the data to the current output stream.
InBlock.gif
                        Response.OutputStream.Write(buffer, 0, length);
InBlock.gif
InBlock.gif                        
// Flush the data to the HTML output.
InBlock.gif
                        Response.Flush();
InBlock.gif
InBlock.gif                        buffer
= new Byte[10240];
InBlock.gif                        dataToRead 
= dataToRead - length;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//prevent infinite loop if user disconnects
InBlock.gif
                        dataToRead = -1;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

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

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (stream != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//Close the file.
InBlock.gif
                    stream.Close();
ExpandedSubBlockEnd.gif                }

InBlock.gif                Response.End();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new EventHandler(this.Page_Load);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/yknb/archive/2006/07/06/444049.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值