求.NET文件下载的源码。
求.NET文件下载的源码,小弟因为对下载有点无知,所以希望高手可以给点解释,多谢。
万分感激。
[解决办法]
文件下载一. 服务端通过Response输出相应的HTTP Response Headers信息,和要下载的文件的数据来把文件发送到客户端,HTTP Response Headers表现在html文件中是下面的形式:
<meta http-equiv= "Content-Type " content= "text/htm ">
http-equiv表示是Headers的名称,content表示这个Headers的值二. 首先,要输出文件的MIME类型:
Page.Response.AddHeader( "Content-Type ", “MIME类型” ); 三. 其次,要输出下载的文件的打开位置和文件名:
Page.Response.AddHeader( "Content-Disposition ", "attachment;filename= " + FileName );
content-disposition 的 HTTP response header 允许指定文档表示的信息。使用这种 header ,你就可以将文档指定成单独打开(而不是在浏览器中打开),还可以根据用户的操作来显示。如果用户要保存文档,你还可以为该文档建议一个文件名。这个建议名称会出现在 Save As 对话框的“文件名”栏中。
打开位置:
attachment —— 表示作为附件发送到客户端,客户端将单独打开此文件。
inline —— 表示将在浏览器中打开这个文件。
文件名:
filename —— 表示发送到客户端文件的文件名。四. 准备发送到客户端的文件数据:1. 先将不同类型来源的数据转成byte类型的数组,再通过Response.BinaryWrite方法发送到客户端:
1.1. 读取文件来获得byte数组: string FileName; //生成或获取要发送到客户端的文件名string filePath = Server.MapPath( "./ ") + FileName; //假设文件在当前目录下if(File.Exists(filePath) == false){ //服务器上没有这个文件 return;}FileStream myFile = File.OpenRead(filePath); //读取文件进入FileStreambyte[] fileCont = new byte[myFile.Length];myFile.Read(fileCont,0,(int)myFile.Length); 将文件流中的内容转成byte数组//
1.2. 在数据库的二进制字段中读取: //从url获取图片的idstring ImageId = Request.QueryString[ "img "];//构建查询语句string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;SqlConnection connection = new SqlConnection( ConfigurationSettings.AppSettings[ "DSN "].ToString() );SqlCommand command = new SqlCommand( sqlText, connection);connection.Open();SqlDataReader dr = command.ExecuteReader();if ( dr.Read()){ byte[] fileCont = (byte[]) dr[ "img_data "] ;}connection.Close();
1.3. 从internet上读取文件: HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create( "http://www.via.com/aa.xls ");HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();Stream readStream = myWebResponse.GetResponseStream(); byte[] bytes = new byte[readStream.Length];bytes = readStream.Read(bytes,0,readStream.Length);
通过上述三种方法获得的文件内容的byte数组就可以用来输出了:
Page.Response.BinaryWrite(fileCont);Page.Response.End();
2. 直接读取文件输出: string FileName; //生成或获取要发送到客户端的文件名string filePath = Server.MapPath( "./ ") + FileName; //假设文件在当前目录下if(File.Exists(filePath) == false){ //服务器上没有这个文件 return;}Page.Response.Clear();Page.Response.AddHeader( "Content-Type ", "image/gif " ); //根据MIME的不同设置Page.Response.AddHeader( "Content-Disposition ", "inline;filename= " + filePath);Page.Response.WriteFile(filePath);Page.Response.End();
[解决办法]
/**//// <summary>
/// 文件下载
/// </summary>
/// <param name= "FullFileName "> </param>
private void FileDownload(string FullFileName)
...{
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType= "application/octet-stream ";
Response.AppendHeader( "Content-Disposition ", "attachment;filename= " +HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader( "Content-Length ",DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
[解决办法]
(一).上传
1.
<INPUT id= "WebFile " style= "WIDTH: 490px; HEIGHT: 22px " type= "file " size= "62 " name= "WebFile " runat= "server ">
protected System.Web.UI.HtmlControls.HtmlInputFile WebFile;
文件上传参考代码:
/// <summary>
/// 文件上传
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
private void BtnUpload_Click(object sender, System.EventArgs e)
{
if(WebFile.PostedFile.FileName== " ")
{
Info.Text= "请先选择要上传的文件 ";
return;
}
try
{
char[] spliter = { '\\ '};
string [] FileName = WebFile.PostedFile.FileName.Split(spliter,10);
string FullPath = CurrentPath + @ "\ " + FileName[FileName.Length-1]; //生成完整文件名
WebFile.PostedFile.SaveAs(FullPath); //保存文件
LoadDir(CurrentPath); //重新载入当前目录
}
catch
{
Info.Text= "上传文件失败,请与管理员联系 ";
}
}
2.
http://www.gdcic.net/dotnetBank/ViewContent.aspx?artid=000000000186
(二).下载
1. C#:
/// <summary>
/// 文件下载
/// </summary>
/// <param name= "FullFileName "> </param>
private void FileDownload(string FullFileName)
{
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType= "application/octet-stream ";
Response.AppendHeader( "Content-Disposition ", "attachment;filename= " +HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader( "Content-Length ",DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
2. vb.net
Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
Try
If File.Exists(page.MapPath(strFileName)) Then
Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
'清除缓冲区流中的所有内容输出.
page.Response.Clear()
'向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
page.Response.AppendHeader( "Content-Disposition ", "attachment; filename= " + page.Server.UrlEncode(TargetFile.Name))
'繁体格式
'page.Response.AppendHeader( "Content-Disposition ", "attachment;filename= " + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))
'向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
page.Response.AppendHeader( "Content-Length ", TargetFile.Length.ToString())
'表明输出的HTTP为流[stream],因此客户端只能下载.
page.Response.ContentType = "application/octet-stream "
'发送文件流到客户端.
page.Response.WriteFile(TargetFile.FullName)
'停止执行当前页
page.Response.End()
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Set save file format:
Response.ContentType = "application/ms-excel ";