Csharp/C#教程:如何通过搜索在WebForm应用程序中打开pdf文件?分享


如何通过搜索在WebForm应用程序中打开pdf文件?

当我点击搜索PDF文件的列表框时,它没有打开。

代码如下。 有什么想法吗?

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.10fbarREPORTCLOTHOH2REPORT", "*" + TextBox1.Text + "*.pdf", SearchOption.AllDirectories); foreach (string file in pdffiles) { // ListBox1.Items.Add(file); ListBox1.Items.Add(Path.GetFileName(file)); } } else { Response.Write("alert('For this Wafer ID Report is Not Generated');"); } } 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(); } 

首先,您必须保存文件的全名以便以后获取。 所以,你必须改变:

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

至:

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

然后,您应该将文件从服务器发送到客户端,如下所示:

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

注意:不要忘记使用AutoPostBack属性设置为true来初始化ListBox

上述就是C#学习教程:如何通过搜索在WebForm应用程序中打开pdf文件?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1002222.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐