首页 新闻 会员 周边 捐助

.net中怎么弹出文件保存对话框

0
[已解决问题] 解决于 2012-04-14 17:01

.net中怎么弹出文件保存对话框

光哥传说的主页 光哥传说 | 菜鸟二级 | 园豆:212
提问于:2012-04-14 09:19
< >
分享
最佳答案
0

//获取文件路径

string filepath = ((Label)e.Item.FindControl("lbFileUrl")).Text;

//下载框
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filepath, System.Text.Encoding.UTF8));

奖励园豆:5
MR.豆耐特 | 菜鸟二级 |园豆:256 | 2012-04-14 09:51

不行啊,试过了,没有弹出保存框啊?

光哥传说 | 园豆:212 (菜鸟二级) | 2012-04-14 09:54

@光哥IT: 写在下载按钮事件里面

MR.豆耐特 | 园豆:256 (菜鸟二级) | 2012-04-14 10:31

@MR.豆耐特: 是呀。。。

光哥传说 | 园豆:212 (菜鸟二级) | 2012-04-14 10:33

@MR.豆耐特: 

 protected void btnDaoChu_Click(object sender, EventArgs e)
    {
        try
        {

            string filepath = "D:\\Personal\\zhengguang.liu\\桌面\\Chap0101.xls";

            //下载框
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filepath, System.Text.Encoding.UTF8));


           
           
            Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream("D:\\Personal\\zhengguang.liu\\桌面\\Chap0101.xls", FileMode.Create));
            document.Open();
            BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
            document.Add(new Paragraph("sdfdfdf", fontChinese));

            iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(Server.MapPath("~/.") + "\\images\\14.gif");
            document.Add(jpeg);
            PdfPTable table = new PdfPTable(datatable.Columns.Count);

            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                for (int j = 0; j < datatable.Columns.Count; j++)
                {
                    table.AddCell(new Phrase(datatable.Rows[i][j].ToString(), fontChinese));
                }
            }
            document.Add(table);

            document.Close();
        }
        catch (DocumentException de)
        {
            Response.Write(de.ToString());
        }
    }

光哥传说 | 园豆:212 (菜鸟二级) | 2012-04-14 10:35
其他回答(1)
0
try
{
  FullFileName = Server.MapPath(FileName);  //FileName--要下载的文件名
  FileInfo DownloadFile= new FileInfo(FullFileName);
  if(DownloadFile.Exists)
{
    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.ASCII));

    Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
    Response.WriteFile(DownloadFile.FullName);
    Response.Flush();

    Response.End();
  }

  else
{
    //文件不存在
  }

  }

catch
{
  //文件不存在
}
悟行 | 园豆:12559 (专家六级) | 2012-04-14 10:32

支持(0) 反对(0) 光哥传说 | 园豆:212 (菜鸟二级) | 2012-04-14 10:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册