::
::
::
::
::
<system.web>
<httpModules>
<add name="UploadModule" type="Bestcomy.Web.Controls.Upload.UploadModule,Bestcomy.Web.Controls.Upload" />
</httpModules>
<httpHandlers>
<add verb="*" path="download.aspx" type="EPWeb.TrafficConfig.DownloadHandler,EPWeb"/>
</httpHandlers>
<httpRuntime maxRequestLength="40000" useFullyQualifiedRedirectUrl="true" executionTimeout="450"/>
调用代码:
using Bestcomy.Web.Controls.Upload;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void Page_Load(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
AspnetUpload upldr = new AspnetUpload();
// upldr.RegisterProgressBar("ProgressBar.aspx",btn_upload);
// upldr.RegisterProgressBar(btn_upload);
// upldr.RegisterModelessProgressBar("ProgressBar.aspx",btn_upload);
upldr.RegisterModelessProgressBar(btn_upload);
string fpath = Path.Combine(Server.MapPath("."),"Upload");
if(!Directory.Exists(fpath))
Directory.CreateDirectory(fpath);
upldr.set_UploadFolder(fpath);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void btn_upload_Click(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
AspnetUpload upldr = new AspnetUpload();
StringBuilder sb = new StringBuilder();
foreach(UploadFile file in upldr.GetUploadFiles("file1"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if(file != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string fpath = Path.Combine(Server.MapPath("."),"Upload");
fpath = Path.Combine(fpath,Path.GetFileName(file.get_FileName()));
file.SaveAs(fpath);
sb.Append("<strong>已录入文件:</strong> ");
string url = QueryFilename.EncryptFilename(Path.GetFileName(file.get_FileName()));
sb.Append(""+Path.GetFileName(file.get_FileName())+" ["+file.get_ContentLength().ToString("###,###")+" Bytes]"+"<br>");//<a href="download.aspx?filename="+url+"" target="_blank"></a>
sb.Append("</div>");
Label1.Text=sb.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
} //if
}//foreach
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
下面是一下下载类。
using System;
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace EPWeb.TrafficConfig
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// DownloadHandler class for large file download.
/// </summary>
public class DownloadHandler : IHttpHandler
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
public DownloadHandler()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Override the ProcessRequest method.
public void ProcessRequest(HttpContext context)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
HttpResponse Response = context.Response;
HttpRequest Request = context.Request;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.IO.Stream iStream = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10240];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Length of the file:
int length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Total bytes to read:
long dataToRead;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
// Identify the file name.
string filename = QueryFilename.DecryptFilename(Request["filename"]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Identify the file to download including its path.
string filepath = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("."),"Upload\"+filename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read);
Response.Clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Total bytes to read:
dataToRead = iStream.Length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long p = 0;
if(Request.Headers["Range"]!=null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Response.StatusCode = 206;
p = long.Parse( Request.Headers["Range"].Replace("bytes=","").Replace("-",""));
}
if(p != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
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=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.GetEncoding(65001).GetBytes(filename)));
// Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iStream.Position = p;
dataToRead = dataToRead - p;
// Read the bytes.
while (dataToRead > 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
// Verify that the client is connected.
if (Response.IsClientConnected)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10240);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Flush the data to the HTML output.
Response.Flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
buffer= new Byte[10240];
dataToRead = dataToRead - length;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (iStream != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//Close the file.
iStream.Close();
}
Response.End();
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Override the IsReusable property.
public bool IsReusable
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return true; }
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
下面是一个加密文件名的类
using System;
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace EPWeb.TrafficConfig
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// QueryFilename class.
/// </summary>
public class QueryFilename
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
private QueryFilename()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static string EncryptFilename(string filename)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
byte[] buffer = HttpContext.Current.Request.ContentEncoding.GetBytes(filename);
return HttpUtility.UrlEncode(Convert.ToBase64String(buffer));
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static string DecryptFilename(string encryptfilename)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
byte[] buffer = Convert.FromBase64String(encryptfilename);
return HttpContext.Current.Request.ContentEncoding.GetString(buffer);
}
}
}
posted on
2007-12-28 14:00
曹雷
阅读(
692)
评论()
编辑
收藏
举报