Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Downloading A File

Reply
Thread Tools

Downloading A File

 
 
=?Utf-8?B?Slppbms=?=
Guest
Posts: n/a
 
      02-19-2004
The code below is used to download a particular file. It works fine with the exception of the downloaded contents is also including the html from the WEB page where the link is setting that executes this code !!! Any ideas

tr

// Open the file
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read )

// Total bytes to read
dataToRead = iStream.Length

Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename)

// Read the bytes
while (dataToRead > 0

// Verify that the client is connected
if (Response.IsClientConnected)

// Read the data in buffer
length = iStream.Read(buffer, 0, 10000)

// Write the data to the current output stream
Response.OutputStream.Write(buffer, 0, length)

// Flush the data to the HTML output
Response.Flush()

buffer= new Byte[10000]
dataToRead = dataToRead - length

els

//prevent infinite loop if user disconnect
dataToRead = -1



catch (Exception ex)

// Trap the error, if any
lblMessage.Text = ex.Message.ToString()

finall

if (iStream != null)

//Close the file
iStream.Close()



 
Reply With Quote
 
 
 
 
Wim Hollebrandse
Guest
Posts: n/a
 
      02-19-2004
Why don't you use the WriteFile method on the Response object to stream the
file to the client???

I.e.

Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" +
filename);
Response.WriteFile(filename);
Response.End();

Hope that helps.

Regards,
--
Wim Hollebrandse
http://www.wimdows.net
http://www.wimdows.com

"JZink" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The code below is used to download a particular file. It works fine with

the exception of the downloaded contents is also including the html from
the WEB page where the link is setting that executes this code !!! Any
ideas ?
>
> try
> {
> // Open the file.
> iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
> System.IO.FileAccess.Read,System.IO.FileShare.Read );
>
>
> // Total bytes to read:
> dataToRead = iStream.Length;
>
> Response.ContentType = "application/octet-stream";
> Response.AddHeader("Content-Disposition", "attachment; filename=" +

filename);
>
> // Read the bytes.
> while (dataToRead > 0)
> {
> // Verify that the client is connected.
> if (Response.IsClientConnected)
> {
> // Read the data in buffer.
> length = iStream.Read(buffer, 0, 10000);
>
> // Write the data to the current output stream.
> Response.OutputStream.Write(buffer, 0, length);
>
> // Flush the data to the HTML output.
> Response.Flush();
>
> buffer= new Byte[10000];
> dataToRead = dataToRead - length;
> }
> else
> {
> //prevent infinite loop if user disconnects
> dataToRead = -1;
> }
> }
> }
> catch (Exception ex)
> {
> // Trap the error, if any.
> lblMessage.Text = ex.Message.ToString();
> }
> finally
> {
> if (iStream != null)
> {
> //Close the file.
> iStream.Close();
> }
> }
>



 
Reply With Quote
 
 
 
 
Patrice Scribe
Guest
Posts: n/a
 
      02-19-2004
A download page should be a separate page that doesn't include any HTML
code. In particular clear the HTML code created by the designer...

The only purpose of this page is to stream the file. You can then use it
from another page :

<a href="downloap.aspx?id=myfile.txt" target="_blank">Download now</A>

You could also use Response.Writefile method for simplicity...

Patrice

--

"JZink" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> The code below is used to download a particular file. It works fine with

the exception of the downloaded contents is also including the html from
the WEB page where the link is setting that executes this code !!! Any
ideas ?
>
> try
> {
> // Open the file.
> iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
> System.IO.FileAccess.Read,System.IO.FileShare.Read );
>
>
> // Total bytes to read:
> dataToRead = iStream.Length;
>
> Response.ContentType = "application/octet-stream";
> Response.AddHeader("Content-Disposition", "attachment; filename=" +

filename);
>
> // Read the bytes.
> while (dataToRead > 0)
> {
> // Verify that the client is connected.
> if (Response.IsClientConnected)
> {
> // Read the data in buffer.
> length = iStream.Read(buffer, 0, 10000);
>
> // Write the data to the current output stream.
> Response.OutputStream.Write(buffer, 0, length);
>
> // Flush the data to the HTML output.
> Response.Flush();
>
> buffer= new Byte[10000];
> dataToRead = dataToRead - length;
> }
> else
> {
> //prevent infinite loop if user disconnects
> dataToRead = -1;
> }
> }
> }
> catch (Exception ex)
> {
> // Trap the error, if any.
> lblMessage.Text = ex.Message.ToString();
> }
> finally
> {
> if (iStream != null)
> {
> //Close the file.
> iStream.Close();
> }
> }
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in downloading exe file from website tushar.n.patel@gmail.com ASP .Net 0 09-26-2005 12:24 PM
Downloading a file from a website Chris Morrison ASP .Net 3 12-31-2003 06:05 AM
Downloading a file Steven ASP .Net 2 08-02-2003 07:10 PM
Downloading a file to client Ian Gordon ASP .Net 2 07-30-2003 10:15 PM
Downloading a file... Ian Gordon ASP .Net 0 07-30-2003 09:17 PM



Advertisments