Hello Guys,
I have a minor problem on my code when I try to create a folder name “Files” to save and view my attached pdf file on my desired path location.
Then save function no problem but when I click View button to generate on new page ViewPdfPage.aspx the Files folder did not read the system and when I try to remove a folder files the view button is no problem.
But What I want is to view the attached file on Files folder because that’s the path folder where I save the attached file on my system.
Please help me.
Here is the code
HTML CODE
Default.aspx
<asp:GridView ID="GridView1" runat="server" Width="1223px" ShowHeaderWhenEmpty="True"
AutoGenerateColumns="False" OnSelectedIndexChanged="OnSelectedIndexChanged"
OnPageIndexChanging="OnPageIndexChanging" DataKeyNames="FilePath">
<AlternatingRowStyle Font-Size="Small" BackColor="#CCCCCC" />
<Columns>
<asp:BoundField ItemStyle-Width="350px" DataField="CENMRPTSCODE" HeaderText="CENMRPTSCODE">
<HeaderStyle ForeColor="#3366CC" />
<ItemStyle Width="350px" />
</asp:BoundField>
<asp:BoundField ItemStyle-Width="150px" DataField="FOLIODES"
HeaderText="FOLIODES">
<HeaderStyle ForeColor="#3366CC" />
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="FileName" HeaderText="FileName">
<HeaderStyle ForeColor="#3366CC" />
</asp:BoundField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="lnkView" Text="View" runat="server" Target="_blank"
NavigateUrl='<%# Eval(HttpUtility.UrlEncode("FileName"), "~/ViewPdfPage.aspx?fileName={0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle Font-Size="Small" />
<HeaderStyle ForeColor="#996633" Font-Size="Small" BackColor="#FFCC66"
BorderColor="#666666" BorderWidth="1px" Height="30px"
HorizontalAlign="Center" VerticalAlign="Middle" />
<RowStyle Font-Size="Small" BackColor="White" />
</asp:GridView>
Vb.Net Code
Private Sub BindGrid()
Using con = OpenCon()
Dim cmd As New SqlCommand("SELECT CENMRPTSCODE, FOLIODES, FileName, FilePath FROM tblCENRO_MontlyRpt", con)
cmd.CommandType = CommandType.Text
Dim dr As SqlDataReader = cmd.ExecuteReader()
GridView1.DataSource = dr
GridView1.DataBind()
End Using
End Sub
Protected Sub GetDocuments()
Dim filePaths As String() = IO.Directory.GetFiles("D:\ARS 03-12-21\PAPIS_XII\Files")
Dim files As List(Of ListItem) = New List(Of ListItem)()
For Each filePath As String In filePaths
files.Add(New ListItem(IO.Path.GetFileName(filePath), filePath))
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BindGrid()
If Not IsPostBack Then
GetDocuments()
End If
End Sub
'Stored Procedured connect to SQL Database
Private Sub Submit()
Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(Server.MapPath("Files/" & filename))
Using con = OpenCon()
Dim cmd As New SqlCommand
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "INSERT_Monthly_Reports"
With cmd.Parameters
.Clear()
.AddWithValue("@CENMRPTSCODE", lbPENRO_Code.Text)
.AddWithValue("@FOLIOCODE", cboFolioDes.SelectedItem.Value)
.AddWithValue("@FileName", filename)
.AddWithValue("@FilePath", "Files/" & filename)
End With
cmd.ExecuteNonQuery()
End Using
End Sub
ViewPdfPage.aspx
VB.NET Code
Public Class ViewPdfPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim filePath As String = Request.QueryString("fileName")
Response.ContentType = "Application/pdf"
Response.WriteFile(filePath)
Response.End()
End If
End Sub
End Class