Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Help passing a list item into file path

Author  Topic 

ph1long
Starting Member

16 Posts

Posted - 2012-10-24 : 13:13:50
Hi,

I have a list view with download file links. When a user clicks on the link an "open,save, cancel" window opens. I need help adding the list items into "filepath" in my code behind. Please help.

ASPx code:

 <ItemTemplate> 
<tr >
<td>
<asp:Label ID="PeriodLabel" runat="server" Text='<%# Eval("Period") %>' />
</td>
<td>

<asp:LinkButton Label ID = "StatementLabel" runat="server" Visible='<%# !string.IsNullOrWhiteSpace(Eval("Statement").ToString()) %>'
NavigateUrl='<%# "~/Reports/" + Eval("Statement") %>' Text='Download' Onclick="LinkButtonDownloadPdf_Click"></asp:LinkButton>

</td>
</tr>
</ItemTemplate>


Code Behind:

protected void LinkButtonDownloadPdf_Click(object sender, EventArgs e) 
{
//Response.ContentType = "Application/pdf";
//Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf");
//Response.TransmitFile(Server.MapPath("~/Reports/FD3_2012-07_Stmt.pdf"));
//Response.End();
// Get the physical Path of the file(test.doc)



string filepath = Server.MapPath("~/Reports/" + Request.QueryString["Statement"]);

// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo file = new FileInfo(filepath);

// Checking if file exists
if (file.Exists)
{
// Clear the content of the response
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();


//Response.Flush();
// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "inline;filename=" + file.Name);

// Add the file size into the response header
Response.AddHeader("Content-Length", file.Length.ToString());

// Set the ContentType
Response.ContentType = ReturnExtension(file.Extension.ToLower());

// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(file.FullName);

// End the response
Response.Buffer = true;
Response.Flush();
Response.End();
}

}
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}
   

- Advertisement -