Following is the normal code for file downloading. But it will not work if button is placed inside update panel.
Syntax(C#):
//string FileResponse="";
string fileName = Request.QueryString["Region"];
string PdfFilePath = "";
PdfFilePath = Server.MapPath("Contracts/" + fileName);
//PdfFilePath = "http://www.thsweb.com/contracts/" + HiddenField2.Value;
if (File.Exists(PdfFilePath))
{
FileInfo fileInfoObj = new FileInfo(PdfFilePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" +fileName);
Response.AddHeader("Content-Length", fileInfoObj.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(fileInfoObj.FullName);
//Response.Write(fileInfoObj.Name);
Response.End();
}
To make it working perfectly we need to add some javascript code on that page where our button to download the file is present.
Following is the code for javascript:
Syntax(Javascript):
function InitializeRequest() {
// Create an IFRAME.
var iframe = document.createElement("iframe");
// Get the desired region from the dropdown.
var region = document.getElementById('clientid of control that contains file name').value;
// Point the IFRAME to GenerateFile, with the
// desired filename as a querystring argument.
iframe.src = "GenerateFile.aspx?region=" + region;
// This makes the IFRAME invisible to the user.
iframe.style.display = "none";
// Add the IFRAME to the page. This will trigger
// a request to GenerateFile now.
document.body.appendChild(iframe);
$get('updaetpanel id').className = "progress";
}
function EndRequest()
{
$get('updaetpanel id').className = "normal";
}
By doing this it will redirect to GenerateFile.aspx page and on its page load it will execute the file downloading code, that i posted at top(start of page).
No comments:
Post a Comment