打开了一个ShowModalDialog窗口在此窗口中要求能够下载对应文件(不限制类型,图片、Excel、Word、RAR均可)
请问应该怎么做?
希望有详细代码
谢谢

解决方案 »

  1.   

    如有办法解决的请发送邮箱[email protected]  谢谢 发送时请注明 csdn昵称哈
      

  2.   

    解决方法是利用页面传值方式(简单吧,就怕没想到),将文件路径和文件名传到另一个页面,然后用Request.QueryString获取值.
    下面代码分别是附件页面和下载页面.
    string slink ="<div><TABLE  border=0><TR><TD&nbsp附件名称</TD><TD>&nbsp附件说明</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>&nbsp&nbsp&nbsp&nbsp</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();
            }
      

  3.   

    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();