ASP.NET文件下载函数使用浅析
ASP.NET文件下载函数使用是什么情况呢?在你的Page_Load中添加这样的代码:
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, "目的文件名称", @"源文件路径", 1024000);
if (!success) Response.Write("下载文件出错!"); Page.Response.End();
ASP.NET文件下载函数代码为:
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed) {
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes"
);
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0; double pack = 10240;
//10K bytes
//int sleep = 200;
//每秒5次 即5*10K bytes每秒 int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0) {
//Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) ); br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++) { if (_Response.IsClientConnected) { _Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString()))); Thread.Sleep(sleep);
} else { i=maxCount;
}
} }
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
这样就实现了文件下
相关文档:
BehaviorEditorPart Web 服务器控件是一个编辑器部件控件,最终用户可以使用该控件在运行时更改关联的 WebPart 控件上的多个用户界面 (UI) 属性。下表列出了用户可自定义的功能,以及用户可使用 BehaviorEditorPart 控件编辑的对应的 WebPart 控件属性值。
用户可自定义的功能
WebPart ......
比较好的数据库访问类
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
namespace PublicClass
{
/// <summary> ///
&n ......
数据绑定控件比较
(Reapter\DataList\GridView\DatailsView\FormView):
1.插入功能方面:
DetailsView和FormView具有插入功能,其它控件没有
2.模板
DataList\FormView\Repeater三种必须编辑模板,而
GridView和DetailsView只有在将列转换成模板列以后才会出现各种模板.
3.自动分页功能
GridView ,DetailsView和Form ......
我觉得这个文章不能算我的原创,因为代码也是我从别的地方找到的,而且我在网上查了,基本上都是这套代码。
通用的把数据倒进word或者excel。这里的导入excel和我之前的那篇文章中的方法是不一样的。
代码如下:
aspx页面中:
<form id="form1" runat="server">
&nbs ......