对话框中下载文件 打开了一个ShowModalDialog窗口在此窗口中要求能够下载对应文件(不限制类型,图片、Excel、Word、RAR均可)请问应该怎么做?希望有详细代码谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如有办法解决的请发送邮箱[email protected] 谢谢 发送时请注明 csdn昵称哈 解决方法是利用页面传值方式(简单吧,就怕没想到),将文件路径和文件名传到另一个页面,然后用Request.QueryString获取值.下面代码分别是附件页面和下载页面.string slink ="<div><TABLE border=0><TR><TD 附件名称</TD><TD> 附件说明</TD></TR>";for(int j =0;j<dt.Rows.Count;j++) { string strpath = Server.UrlEncode(GlobalVar.UploadPath+ "/" +dt.Rows[j]["Filename"].ToString()); string strfilename = Server.UrlEncode(dt.Rows[j]["Filename"].ToString()); string strUrl = "FileDownLoad.aspx?pathname="+strpath + "&filename="+ strfilename; slink += "<TR><TD><a href=\""+ strUrl +"\" target=\"_blank\")>"+ dt.Rows[j]["Filename"].ToString() +"</a>    </TD><TD>"+dt.Rows[j]["Explain"].ToString()+"</TD></TR>"; }其中的dt 是附件表,GlobalVar.UploadPath是附件地址,Server.UrlEncode是为了中文附件名的传值.FileDownLoad.aspx的.cs文件如下(FileDownLoad没有用BasePage的)private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 string sfilePath = Request.QueryString["pathname"]; string sfilename = Request.QueryString["filename"]; Response.Clear(); Response.Charset = "utf-8"; Response.Buffer= true; this.EnableViewState = false; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(sfilename, System.Text.Encoding.UTF8)); Response.WriteFile(sfilePath); Response.Flush(); Response.Close(); Response.End(); } string filePath ="";FileInfo Fi = new FileInfo(filePath);if (Fi.Exists){ FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=1.excel"); Response.BinaryWrite(bytes); Response.Flush(); Response.End();}string path = Server.MapPath("~/") + "";Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(obj.Name, System.Text.Encoding.GetEncoding("utf-8")));Response.ContentType = "application/octet-stream";Response.WriteFile("" + path + "");Response.End(); 正由另一进程使用,因此该进程无法访问该文件 求解。。。很急 用ASP.NET开发可浮动的QQ客服 获取QQ好友列表,有谁有经验的.>>?指教? 关于.Net Remoting实现分布式数据库 ViewState()的值是存在什么地方,是服务器的内存还是客户端IE里。(另带二个小问题) 关于外部链接绑定控件上的问题! asp.net獲得客戶端電腦名稱 怎样使treeview 控件在panel 控件中显示 上下、左右 滚动条? 求救,在线等待 高分求助~~~~!!! 100分求 IIS访问权限设置的几个问题
下面代码分别是附件页面和下载页面.
string slink ="<div><TABLE border=0><TR><TD 附件名称</TD><TD> 附件说明</TD></TR>";
for(int j =0;j<dt.Rows.Count;j++)
{
string strpath = Server.UrlEncode(GlobalVar.UploadPath+ "/" +dt.Rows[j]["Filename"].ToString());
string strfilename = Server.UrlEncode(dt.Rows[j]["Filename"].ToString());
string strUrl = "FileDownLoad.aspx?pathname="+strpath + "&filename="+ strfilename;
slink += "<TR><TD><a href=\""+ strUrl +"\" target=\"_blank\")>"+ dt.Rows[j]["Filename"].ToString() +"</a>    </TD><TD>"+dt.Rows[j]["Explain"].ToString()+"</TD></TR>";
}其中的dt 是附件表,GlobalVar.UploadPath是附件地址,Server.UrlEncode是为了中文附件名的传值.
FileDownLoad.aspx的.cs文件如下(FileDownLoad没有用BasePage的)private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string sfilePath = Request.QueryString["pathname"];
string sfilename = Request.QueryString["filename"];
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer= true;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(sfilename, System.Text.Encoding.UTF8));
Response.WriteFile(sfilePath);
Response.Flush();
Response.Close();
Response.End();
}
FileInfo Fi = new FileInfo(filePath);
if (Fi.Exists)
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=1.excel");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
string path = Server.MapPath("~/") + "";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(obj.Name, System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + path + "");
Response.End();