如何打开通过搜索一个WebForm应用程序中的PDF文件? [英] How to open a pdf file in a WebForm application by search?

查看:97
本文介绍了如何打开通过搜索一个WebForm应用程序中的PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击该搜索的PDF文件列表框,它是不开放。

在code如下。有什么想法?

 保护无效的button1_Click(对象发件人,EventArgs的发送)
{
  ListBox1.Items.Clear();
  字符串搜索= TextBox1.Text;
  如果(TextBox1.Text!=)
  {
    字符串[] = pdffiles Directory.GetFiles(@\\\\ 192.168.5.10 \\ FBAR \\报告\\克洛托\\ H2 \\报告\\,*+ TextBox1.Text +* .PDF,SearchOption.AllDirectories);
    的foreach(在pdffiles字符串的文件)
    {
      // ListBox1.Items.Add(文件);
      ListBox1.Items.Add(Path.GetFileName(文件));
    }
  }
  其他
  {
    的Response.Write(<脚本>警报('对于这个晶圆ID报表不产生');< / SCRIPT>中);
  }
}保护无效ListBox1_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
  字符串pdffiles = ListBox1.SelectedItem.ToString();  的String.Format(附件;文件名= {0},文件名));  的ProcessStartInfo infoOpenPdf =新的ProcessStartInfo();
  infoOpenPdf.FileName = pdffiles;
  infoOpenPdf.Verb =OPEN;
  //的Process.Start(文件);
  infoOpenPdf.CreateNoWindow = TRUE;
  infoOpenPdf.WindowStyle = ProcessWindowStyle.Normal;  流程openPdf =新的Process();
  openPdf.StartInfo = infoOpenPdf;
  openPdf.Start();
}


解决方案

首先,你必须保存该文件的全名后得到它。所以,你必须从改变:

  ListBox1.Items.Add(Path.GetFileName(文件));

要:

  ListBox1.Items.Add(新的ListItem(Path.GetFileName(文件),文件));


然后,您应该从服务器发送文件到客户端,像这样的:

 保护无效ListBox1_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    字符串文件名= ListBox1.SelectedValue;
    字节[] = fileBytes System.IO.File.ReadAllBytes(文件名);    System.Web.HttpContext语境= System.Web.HttpContext.Current;
    context.Response.Clear();
    context.Response.ClearHeaders();
    context.Response.ClearContent();
    context.Response.AppendHeader(内容长度,fileBytes.Length.ToString());
    context.Response.ContentType =应用程序/ PDF
    context.Response.AppendHeader(内容处置,附件;文件名=+文件名);
    context.Response.BinaryWrite(fileBytes);
    context.ApplicationInstance.CompleteRequest();
}


请注意:不要忘了初始化的ListBox 与物业的AutoPostBack 设置好的为真正

When I click on the listbox which search a PDF file, it's not opening.

The code is below. Any thoughts?

protected void Button1_Click(object sender, EventArgs e)
{
  ListBox1.Items.Clear();
  string search = TextBox1.Text;
  if (TextBox1.Text != "") 
  {
    string[] pdffiles = Directory.GetFiles(@"\\192.168.5.10\fbar\REPORT\CLOTHO\H2\REPORT\", "*" + TextBox1.Text + "*.pdf", SearchOption.AllDirectories);
    foreach (string file in pdffiles)
    {
      // ListBox1.Items.Add(file);
      ListBox1.Items.Add(Path.GetFileName(file));
    }
  }
  else
  {
    Response.Write("<script>alert('For this Wafer ID Report is Not Generated');</script>");
  }
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  string pdffiles = ListBox1.SelectedItem.ToString();

  string.Format("attachment; filename={0}", fileName));

  ProcessStartInfo infoOpenPdf = new ProcessStartInfo();
  infoOpenPdf.FileName = pdffiles;
  infoOpenPdf.Verb = "OPEN";
  // Process.Start(file);
  infoOpenPdf.CreateNoWindow = true;
  infoOpenPdf.WindowStyle = ProcessWindowStyle.Normal;

  Process openPdf = new Process();
  openPdf.StartInfo = infoOpenPdf;
  openPdf.Start();
}

解决方案

First, you must save the file's full name to get it later. So, you must change from:

ListBox1.Items.Add(Path.GetFileName(file));

To:

ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file));


Then, you should send the file from the server to the client, like this:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string fileName = ListBox1.SelectedValue;
    byte[] fileBytes = System.IO.File.ReadAllBytes(fileName);

    System.Web.HttpContext context = System.Web.HttpContext.Current;
    context.Response.Clear();
    context.Response.ClearHeaders();
    context.Response.ClearContent();
    context.Response.AppendHeader("content-length", fileBytes.Length.ToString());
    context.Response.ContentType = "application/pdf";
    context.Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
    context.Response.BinaryWrite(fileBytes);
    context.ApplicationInstance.CompleteRequest();
}


Note: don't forget to initialize your ListBox with the property AutoPostBack setted to true.

这篇关于如何打开通过搜索一个WebForm应用程序中的PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆