asp.net 文件下载功能函数代码整理_实用技巧

复制代码 代码如下:

public void FileDownLoadDel(string fullFilename)
{
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file to download including its path.
string filepath = fullFilename;
filepath = Server.MapPath(filepath);
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);

try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);

// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// 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[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
Response.Clear();
}
}
Response.End(); //没有这句会将该页面刷新后的内容追加写入文件中。
}
catch (Exception ex)
{
// Trap the error, if any.
//Response.Write("Error : " + ex.Message);
//base.WriteLog("资料", "下载资料:" + ex.Message + "!", LogType.Error, this.GetType().ToString());
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
File.Delete(fullFilename);
}
}

时间: 2024-09-12 04:28:43

asp.net 文件下载功能函数代码整理_实用技巧的相关文章

ASP.NET程序中常用代码汇总_实用技巧

1. 打开新的窗口并传送参数: //传送参数: response.write("<script>window.open('*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="++"')</script>") //接收参数: string a = Request.QueryString("id"); string b = Request.QueryS

asp.net ASPxTextBox等控件实现&amp;quot;回车模拟Tab&amp;quot;的 常用代码整理_实用技巧

近期在做一个Web项目,我使用DevExpress第三方控件. 由于该控件使用技巧中文资料较少,还真够呛的,只能边摸索,边开发. 今天我要实现一些编辑框如ASPxTextBox.ASPxComboBox等控件回车模拟Tab的功能.这没办法,用户用惯了回车,讨厌按Tab来移动焦点(鼠标点击更麻烦). 以ASPxTextBox为例,在ClientSideEvents属性中可设置许多客户端JavaScript代码,其中KeyPress就是我要写的. 首先,得准备要模拟Tab的JS代码,这网上很多,我随

ASP.NET 页面刷新和定时跳转代码整理_实用技巧

刷新的实现方法: =========================================== .net //第1: /*-- 内置跳转 ----*/ Response.Redirect( Request.Url.ToString( ) ); //第2: /*----内置 html ----*/ Response.AddHeader( "Refresh","0" ); //将指定的标头和值添加到此响应的 HTTP 标头. //第3: Response.Wr

.NET 常用功能和代码小结_实用技巧

1. 打开新的窗口并传送参数: 传送参数: response.write("<script>window.open('*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"')</script>") 接收参数: string a = Request.QueryString("id"); string b = Request.QuerySt

asp.net的cms 核心代码篇_实用技巧

第一篇,我简略描述了一下我的cms标签所表示的含义.anCMS(c#版)第一篇绑定数据 第二篇,我将展示了标签背后真正运行的代码.asp.net的cms 原理篇 好像开源有点多余,核心代码就下面这些. 复制代码 代码如下: using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace an.helper { ///

ASP.NET餐饮管理系统制作代码分享_实用技巧

页面介绍展示: 以上是餐饮管理系统制作图片介绍,接下来是代码部分. menu.aspx  <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="menu.aspx.cs" Inherits="menu" Title="无标题页" %> <

asp.net导出Excel类库代码分享_实用技巧

复制代码 代码如下: using System;using System.Collections.Generic;using System.Reflection;using System.Web;using Excel = Microsoft.Office.Interop.Excel; /// <summary>///ExcelClass 的摘要说明/// </summary>public class ExcelClass{    /// <summary>    //

在ASP.NET中插入flash代码实例_实用技巧

在需要插入Flash的地方插入以下代码: 复制代码 代码如下: <OBJECT codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"         classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">        <PARAM NAME="_cx

ASP转换格林威治时间函数DateDiff()应用_应用技巧

我今天介绍的是在ASP中如果把时间(例如:2008-8-8 13:45:22)转换成秒的函数,和相反的把秒转换成时间的函数.当然时间转换的时候.必须有一个相对的固定时间,那就是格林威治时间.大家想想因为格林威治时间是以" 1970 年 1 月 1 日 00:00:00 "开始的,ASP提供了一个叫 DateDiff() 的函数,这个函数可以返回一个时间差的秒,那就是说我们放进去一个格林威治标准时间与现在的时间对比一下返回秒就OK.但我们还要注意时区,我们中国是GMT+08:00:00(