my web form creates a file and saves it to a folder in the server that the user can click on and view it in browser:
<asp:Repeater runat="server" ID="rptContent" OnItemCommand="btnGeneratePDF_Click">
<HeaderTemplate>
<table id="tblResponse">
<tr id="hdrRow">
<td style="width: 25%;">Name</td>
<td style="width: 25%;">Last Four SSN #</td>
<td style="width: 25%;">PDF Generator</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="trNormal">
<td><%# Eval("name").ToString() %></td>
<td><%# Eval("ssn3").ToString() %></td>
<td><asp:Button ID="btnGeneratePDF" ClientIDMode="Static" runat="server" Text="Generate PDF" CommandArgument='<%# Eval("name").ToString() + ", " + Eval("ssn3").ToString() %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
When the user clicks on the Generate PDF button it currently displays a link to the file the user can click on to view the file. The code behind is here:
public void writeData(string k, string c)
{
Conn = new SqlConnection(cString);
Conn.Open();
string pdfTemplate = Path.Combine(Server.MapPath("~/PDFTemplates/forme.pdf"));
//MessageBox.Show(pdfTemplate);
string newFile = strDirectory + "completed_pdf_" + k + ".pdf";
newFileServer = System.Environment.MachineName + @"/PDFGenerate/completed_pdf_" + k + ".pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
//if more than multiple entries, verify by name and the last four ssn
sqlCode = "SELECT * FROM [Db].[dbo].[TablePDF] WHERE [name] = '" + k + "' AND [ssn3] = " + c + "";
//MessageBox.Show("" + sqlCode.ToString());
using (SqlCommand command = new SqlCommand(sqlCode, Conn))
{
command.CommandType = CommandType.Text;
using (reader = command.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
pdfFormFields.SetField("lName", reader.GetValue(0).ToString());
pdfFormFields.SetField("fName", reader.GetValue(1).ToString());
pdfFormFields.SetField("cbMALEpg3", "Yes");
tc.Text = "A completed PDF for " + k + " was generated successfully and saved to <a target=_blank href=/PDFGenerate/completed_pdf_" + k + ".pdf>//" + newFileServer + "</a>";
strFullPath = Path.GetFullPath("//" + newFileServer);
List<System.Web.UI.WebControls.ListItem> files = new List<System.Web.UI.WebControls.ListItem>();
files.Add(new System.Web.UI.WebControls.ListItem(strFullPath, strFullPath));
GridView1.DataSource = files;
GridView1.DataBind();
}
}
}
}
pdfStamper.FormFlattening = false; //allow user to modify the form once it has been saved. Set to TRUE otherwise.
// close the pdf
pdfStamper.Close();
Conn.Close();
}
The above codes work fine, where the button generates a link in the tc
label and I am able to click and view the file in the browser.
The tc
label shows this which is a link I can click on and view the pdf file:
A completed PDF for bill was generated successfully and saved to <a href="http://server:85/PDFGenerate/completed_pdf_bill.pdf">//server/PDFGenerate/completed_pdf_bill.pdf</a>
Side note: I created a virtual folder in my site in IIS pointing to the physical folder in the server which stores the files, making the above link work.
The GridView
code portion above is to populate a table with the link and a DOWNLOAD and VIEW option that I added on my page here:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText = "No PDF was generated">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text = "Download" runat="server" OnClick = "DownloadFile" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID = "lnkView" Text = "View" runat = "server" OnClick = "ViewFile" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I also added the following code to handle the DOWNLOAD command:
protected void DownloadFile(object sender, EventArgs e)
{
string strFilePath = Path.GetFullPath("\\\\" + newFileServer);
MessageBox.Show(strFilePath);
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFullPath);
MessageBox.Show(strFullPath);
Response.WriteFile(strFilePath);
Response.End();
}
I will be accessing that outside of the server that it is on from a local PC which is still in the same network. When I click on the DOWNLOAD link, I get an error: The UNC path should be of the form \\server\share
Right now, the GridView
is populating correctly, but the DOWNLOAD link isn't working inside the GridView
.
How can I fix the code so the DOWNLOAD link works? Also, what would be the code so make the VIEW link work the way it is currently working in the tc
label?