动态生成LinkButton控件,动态添加事件调用一个方法Public Sub Download(ByVal oResponse As System.Web.HttpResponse, ByVal FileName As String, ByVal strFileNamePrompt As String) Dim oFile As System.IO.FileInfo = New System.IO.FileInfo(FileName) oResponse.AddHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode(strFileNamePrompt)) oResponse.AddHeader("Content-Length", oFile.Length.ToString()) oResponse.ContentType = "APPLICATION/OCTET-STREAM" oResponse.WriteFile(oFile.FullName) oResponse.End() End Sub
public void DownLoadFiles(string fullName) { // retrieve the path of the file to download, and create // a FileInfo object to read its properties string path = Server.MapPath("../Files/"+fullName); System.IO.FileInfo file = new System.IO.FileInfo(path); string subName = file.Name.Substring(14); string fileName = subName.Substring(0,subName.LastIndexOf(".")); string Ext = subName.Substring(subName.LastIndexOf(".")); // clear the current output content from the buffer Response.Clear(); // add the header that specifies the default filename for the Download/SaveAs dialog //设置此句,防止下载对话框中的选项在中文标题时,出现乱码。 Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName)+ Ext); // add the header that specifies the file size, so that the browser // can show the download progress
//设置为中文,防止乱码,相当于Response.AddHeader("Content-type", "text/html;Charset=GB2312"); Response.Charset = "GB2312"; //设置输出流为简体中文输出流。 Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); Response.AddHeader("Content-Length", file.Length.ToString()); // specify that the response is a stream that cannot be read by the // client and must be downloaded Response.ContentType = "application/octet-stream"; // send the file stream to the client Response.WriteFile(file.FullName); // stop the execution of this page Response.End(); }
String[] files = Directory.GetFiles(directoryPath);
遍历数组,得到文件的名字,生成这样的格式
down.aspx?name=filename关于下载,网上有很多代码,在此不重复。搜索asp.net 下载文件
Dim oFile As System.IO.FileInfo = New System.IO.FileInfo(FileName)
oResponse.AddHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode(strFileNamePrompt))
oResponse.AddHeader("Content-Length", oFile.Length.ToString())
oResponse.ContentType = "APPLICATION/OCTET-STREAM"
oResponse.WriteFile(oFile.FullName)
oResponse.End()
End Sub
http://support.microsoft.com/kb/812406/zh-cn
{
// retrieve the path of the file to download, and create
// a FileInfo object to read its properties
string path = Server.MapPath("../Files/"+fullName);
System.IO.FileInfo file = new System.IO.FileInfo(path);
string subName = file.Name.Substring(14);
string fileName = subName.Substring(0,subName.LastIndexOf("."));
string Ext = subName.Substring(subName.LastIndexOf("."));
// clear the current output content from the buffer
Response.Clear();
// add the header that specifies the default filename for the Download/SaveAs dialog //设置此句,防止下载对话框中的选项在中文标题时,出现乱码。
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName)+ Ext);
// add the header that specifies the file size, so that the browser
// can show the download progress
//设置为中文,防止乱码,相当于Response.AddHeader("Content-type", "text/html;Charset=GB2312");
Response.Charset = "GB2312";
//设置输出流为简体中文输出流。
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); Response.AddHeader("Content-Length", file.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded
Response.ContentType = "application/octet-stream";
// send the file stream to the client
Response.WriteFile(file.FullName);
// stop the execution of this page
Response.End(); }