Downloading A File

G

Guest

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()
 
W

Wim Hollebrandse

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 said:
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();
}
}
 
P

Patrice Scribe

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 said:
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();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top