[英]Browse to a PDF in ASP.NET Web Site Folder
I have an ASP.NET Core web site running under IIS on a server.我有一个 ASP.NET 核心 web 站点在服务器上的 IIS 下运行。 I created a folder and want to display any pdf file in that folder in the browser using the url for the site.我创建了一个文件夹,并希望使用该站点的 url 在浏览器的该文件夹中显示任何 pdf 文件。
https://example.com/pdf/myfile.pdf https://example.com/pdf/myfile.pdf
folder structure: c:\inetpub\wwwroot\mysite\pdf\myfile.pdf - contains pdf files文件夹结构:c:\inetpub\wwwroot\mysite\pdf\myfile.pdf - 包含 pdf 文件
I enabled Directory Browsing for the site in IIS Manager, but I get a 404 error.我在 IIS 管理器中为该站点启用了目录浏览,但出现 404 错误。 I'm on Window Server 2019 IIS version 10.我在 Window 服务器 2019 IIS 版本 10 上。
I would dispense with folder/directory browsing.我会省去文件夹/目录浏览。 You can't control the user experince and UI.您无法控制用户体验和 UI。
And it just we few lines of code to display the files in a web page, and then have a button to display the pdf in the browser.只需几行代码就可以在 web 页面中显示文件,然后在浏览器中显示 pdf 的按钮。
So our markup is like this:所以我们的标记是这样的:
<h2>Select a PDF file to view</h2>
<br />
<div style="width:45%">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="table table-hover">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="100px" />
<asp:BoundField DataField="File Name" HeaderText="ID" />
<asp:BoundField DataField="File Size" HeaderText="Size" ItemStyle-Width="100px" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="cmdDownLoad" runat="server" Text="View"
OnClick="cmdDownLoad_Click"
Row = '<%# Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
And our code to pull files from a folder and shove into the above gridview looks like this:我们从文件夹中提取文件并将其推入上述 gridview 的代码如下所示:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
ShowFiles()
End If
End Sub
Sub ShowFiles()
Dim MyTable As New DataTable
MyTable.Columns.Add("Date", GetType(String))
MyTable.Columns.Add("File Size", GetType(String))
MyTable.Columns.Add("File Name", GetType(String))
MyTable.Columns.Add("FullFile", GetType(String))
'Dim strFolder = Server.MapPath("/app_data")
Dim strFolder = "c:\Test4"
Dim MyDir As New DirectoryInfo(strFolder)
Dim MyFiles() As FileInfo = MyDir.GetFiles("*.pdf")
For Each MyFile As FileInfo In MyFiles
Dim oneRow As DataRow = MyTable.Rows.Add
oneRow("Date") = MyFile.LastAccessTime.ToShortDateString
oneRow("File Size") = Int(MyFile.Length / 1024) & " KB"
oneRow("File Name") = MyFile.Name
oneRow("FullFile") = MyFile.FullName
Next
ViewState("MyTable") = MyTable
GridView1.DataSource = MyTable
GridView1.DataBind()
End Sub
The result is thus this:结果是这样的:
So, now all we need is the button code to display the pdf in the browser.所以,现在我们需要的只是在浏览器中显示 pdf 的按钮代码。
This will work:这将起作用:
Protected Sub cmdDownLoad_Click(sender As Object, e As EventArgs)
Dim btn As Button = sender
Dim RowID = btn.Attributes.Item("Row")
Dim MyTable As DataTable = ViewState("MyTable")
With MyTable.Rows(RowID)
ShowPdf(.Item("File Name"), .Item("FullFile"))
End With
End Sub
Sub ShowPdf(strFileOnlyName As String, strFile As String)
Dim pDFData As Byte()
pDFData = File.ReadAllBytes(strFile)
Response.Buffer = False ' //transmitfile self buffers
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.AddHeader("Accept-Header", pDFData.Length.ToString())
Response.AddHeader("Content-Length", pDFData.Length.ToString())
Response.AddHeader("Content-Disposition", "inline; filename=" & strFileOnlyName)
Response.AddHeader("Expires", "0")
Response.AddHeader("Cache-Control", "private")
Response.ContentType = "application/pdf"
Response.AddHeader("Accept-Ranges", "bytes")
Response.BinaryWrite(pDFData)
Response.Flush()
Response.End()
End Sub
So you can click on the button - it will display the PDF in the browser.因此,您可以单击该按钮 - 它会在浏览器中显示 PDF。 If you hit back, then you back to the grid, and you can then view another file.如果你回击,那么你回到网格,然后你可以查看另一个文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.