Load a wen page with file information and show save as dialog

  • Thread starter Thread starter Stefan Soljemo
  • Start date Start date
S

Stefan Soljemo

I developing an asp.net application with a page that creating a file.
Redirect to another page with file information and shows a save as dialog.
The file will be streamed as contets type "application/force-download". The
first questeion is if I need to create the page twise. One with the HTML data
to be shown and a second with the sream. If so how to do the postback?
The second question is if its possible to att the contents type
"application/force-download" at the end of the page.
Note that the application using AJAX.

I use the folliwing code to create the stream:
stream = new FileStream( sFile, FileMode.Open, FileAccess.Read,
FileShare.Read );

Response.Clear();
Response.AddHeader( "Content-Disposition", "attachment; filename=" + sFile);
Response.AddHeader( "Content-Length", stream.Length.ToString() );
Response.ContentType = "application/force-download";
iBufSize = 16384;
buffer = new byte[ iBufSize ];
iCount = stream.Read( buffer, 0, iBufSize );

while( iCount > 0 )
{
Response.OutputStream.Write( buffer, 0, iCount );
iCount = stream.Read( buffer, 0, iBufSize );
}
Response.Flush();
Response.Close();
stream.Close();
 
I developing an asp.net application with a page that creating a file.
Redirect to another page with file information and shows a save as dialog..
The file will be streamed as contets type "application/force-download". The
first questeion is if I need to create the page twise. One with the HTML data
to be shown and a second with the sream. If so how to do the postback?
The second question is if its possible to att the contents type
"application/force-download" at the end of the page.
Note that the application using AJAX.

I use the folliwing code to create the stream:
stream = new FileStream( sFile, FileMode.Open, FileAccess.Read,
FileShare.Read );

Response.Clear();
Response.AddHeader( "Content-Disposition", "attachment; filename=" + sFile);
Response.AddHeader( "Content-Length", stream.Length.ToString() );
Response.ContentType = "application/force-download";
iBufSize = 16384;
buffer = new byte[ iBufSize ];
iCount = stream.Read( buffer, 0, iBufSize );

while( iCount > 0 )
{
    Response.OutputStream.Write( buffer, 0, iCount );
    iCount = stream.Read( buffer, 0, iBufSize );}

Response.Flush();
Response.Close();
stream.Close();

I think you need to put your code in the place where redirect was
located. You can also add a control such as LinkButton to do the
postback. For example,

<asp:LinkButton id="LinkButton1" Text="Download"....

void LinkButton1_Click(...)
{
stream = new FileStream( sFile, FileMode.Open, FileAccess.Read,
FileShare.Read );
Response.Clear();
.....

}

Response.ContentType specifies the HTTP content type for the response
header and this information is located at the top (head) of the
response

HTTP/1.1 200 OK
Content-Disposition: filename=xxx
Content-Length: xxx
Content-Type: application/force-download
.....
 
Thank you Alexey!

I did that from begining but got trouble when tha page was loaded twise
because of security settings. The dtream needs to be created every time tha
page is loaded.




Stefan Soljemo said:
I developing an asp.net application with a page that creating a file.
Redirect to another page with file information and shows a save as dialog.
The file will be streamed as contets type "application/force-download". The
first questeion is if I need to create the page twise. One with the HTML data
to be shown and a second with the sream. If so how to do the postback?
The second question is if its possible to att the contents type
"application/force-download" at the end of the page.
Note that the application using AJAX.

I use the folliwing code to create the stream:
stream = new FileStream( sFile, FileMode.Open, FileAccess.Read,
FileShare.Read );

Response.Clear();
Response.AddHeader( "Content-Disposition", "attachment; filename=" + sFile);
Response.AddHeader( "Content-Length", stream.Length.ToString() );
Response.ContentType = "application/force-download";
iBufSize = 16384;
buffer = new byte[ iBufSize ];
iCount = stream.Read( buffer, 0, iBufSize );

while( iCount > 0 )
{
Response.OutputStream.Write( buffer, 0, iCount );
iCount = stream.Read( buffer, 0, iBufSize );
}
Response.Flush();
Response.Close();
stream.Close();

I think you need to put your code in the place where redirect was
located. You can also add a control such as LinkButton to do the
postback. For example,

<asp:LinkButton id="LinkButton1" Text="Download"....

void LinkButton1_Click(...)
{
stream = new FileStream( sFile, FileMode.Open, FileAccess.Read,
FileShare.Read );
Response.Clear();
......

}

Response.ContentType specifies the HTTP content type for the response
header and this information is located at the top (head) of the
response

HTTP/1.1 200 OK
Content-Disposition: filename=xxx
Content-Length: xxx
Content-Type: application/force-download
......



Alexey Smirnov
 
Thank you Alexey!

I did that from begining but got trouble when tha page was loaded twise
because of security settings. The dtream needs to be created every time tha
page is loaded.


You probably can use Page.IsPostBack Property

if (!Page.IsPostBack) {
// here's the code with the HTML data
}
else {
// here's the code with the sream.
stream = new FileStream( sFile, FileMode.Open, FileAccess.Read,
FileShare.Read );
}

void LinkButton1_Click(...)
{

Response.Clear();
Response.Clear();
Response.AddHeader( "Content-Disposition", "attachment; filename=" +
sFile);
Response.AddHeader( "Content-Length", stream.Length.ToString() );
.....

}



}
 
Back
Top