G
Guest
Basically I have a link that opens my download page and the querystring
identifies the type of 'template' Excel spreadsheet has asked to download.
The download page reads the querystring, identifies the template required
then uses Response.AppendHeader to amend the response to create the file
download dialogue. On my local machine this works fine and the download
dialogue correctly identifies the file name and type attempting to be
downloaded. However, when this is ported to the Test web server the download
dialogue asks if I want to save or open the Download.aspx page, not the xls
file that should be targetted. To further confuse me, if you save this file,
open it in a text editor, remove the html elements of it, leave the encoded
elements, (square blocks, hex etc.), and save this as an xls file from the
text editor MS Excel opens the saved file correctly and it is in fact the
file that was attempted to be downloaded.
To me it appears that on my dev machine the download works fine and streams
the excel file to be saved locally, but on the test server it's streaming
both the page and the Excel file for downloading. I've included the basics of
the code below and was wondering if anyone has a way to explain this
behaviour and how to fix it...
<code>
private void Page_Load(object sender, System.EventArgs e)
{
if( Request.QueryString["filetype"] != null )
{
// A file download has been requested
string downloadType =
Request.QueryString["filetype"].ToString().ToUpper();
if( downloadType ==
FRSessionParameterName.DownloadSRPTemplate.ToUpper() )
{
string filePath =
ConfigurationSettings.AppSettings["templatesDir"] +
"/" +
ConfigurationSettings.AppSettings["stormReturnPeriodsDir"] +
"/" +
ConfigurationSettings.AppSettings["stormReturnPeriodsTemplate"];
filePath = this.MapPath( filePath );
DownloadFile( filePath, true );
}
else
{
// Unknown file type - shouldn't happen
}
}
}
private void DownloadFile( string path, bool forceDownload )
{
string name = Path.GetFileName( path );
string ext = Path.GetExtension( path );
string type = "";
// set known types based on file extension
if ( ext != null )
{
switch( ext.ToLower() )
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case ".xls":
type = "Application/vnd.ms-excel";
break;
default:
type = "text/plain";
break;
}
}
if ( forceDownload )
{
Response.AppendHeader( "content-disposition", "attachment;
filename=" + name );
}
if ( type != "" )
{
Response.ContentType = type;
}
// Force file to download
Response.WriteFile(path);
Response.End();
}
</code>
identifies the type of 'template' Excel spreadsheet has asked to download.
The download page reads the querystring, identifies the template required
then uses Response.AppendHeader to amend the response to create the file
download dialogue. On my local machine this works fine and the download
dialogue correctly identifies the file name and type attempting to be
downloaded. However, when this is ported to the Test web server the download
dialogue asks if I want to save or open the Download.aspx page, not the xls
file that should be targetted. To further confuse me, if you save this file,
open it in a text editor, remove the html elements of it, leave the encoded
elements, (square blocks, hex etc.), and save this as an xls file from the
text editor MS Excel opens the saved file correctly and it is in fact the
file that was attempted to be downloaded.
To me it appears that on my dev machine the download works fine and streams
the excel file to be saved locally, but on the test server it's streaming
both the page and the Excel file for downloading. I've included the basics of
the code below and was wondering if anyone has a way to explain this
behaviour and how to fix it...
<code>
private void Page_Load(object sender, System.EventArgs e)
{
if( Request.QueryString["filetype"] != null )
{
// A file download has been requested
string downloadType =
Request.QueryString["filetype"].ToString().ToUpper();
if( downloadType ==
FRSessionParameterName.DownloadSRPTemplate.ToUpper() )
{
string filePath =
ConfigurationSettings.AppSettings["templatesDir"] +
"/" +
ConfigurationSettings.AppSettings["stormReturnPeriodsDir"] +
"/" +
ConfigurationSettings.AppSettings["stormReturnPeriodsTemplate"];
filePath = this.MapPath( filePath );
DownloadFile( filePath, true );
}
else
{
// Unknown file type - shouldn't happen
}
}
}
private void DownloadFile( string path, bool forceDownload )
{
string name = Path.GetFileName( path );
string ext = Path.GetExtension( path );
string type = "";
// set known types based on file extension
if ( ext != null )
{
switch( ext.ToLower() )
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case ".xls":
type = "Application/vnd.ms-excel";
break;
default:
type = "text/plain";
break;
}
}
if ( forceDownload )
{
Response.AppendHeader( "content-disposition", "attachment;
filename=" + name );
}
if ( type != "" )
{
Response.ContentType = type;
}
// Force file to download
Response.WriteFile(path);
Response.End();
}
</code>