Downloading A File

Discussion in 'ASP .Net' started by =?Utf-8?B?Slppbms=?=, Feb 19, 2004.

  1. 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()
    =?Utf-8?B?Slppbms=?=, Feb 19, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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();
    > }
    > }
    >
    Wim Hollebrandse, Feb 19, 2004
    #2
    1. Advertising

  3. 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" <> a écrit dans le message de
    news:...
    > 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();
    > }
    > }
    >
    Patrice Scribe, Feb 19, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Gordon

    Downloading a file...

    Ian Gordon, Jul 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    933
    Ian Gordon
    Jul 30, 2003
  2. Chris Morrison

    Downloading a file from a website

    Chris Morrison, Dec 18, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    500
    Vijayakrishna Pondala
    Dec 31, 2003
  3. Replies:
    0
    Views:
    399
  4. Steven

    Downloading a file

    Steven, Jul 28, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    382
    Joerg Jooss
    Aug 2, 2003
  5. Ian Gordon

    Downloading a file to client

    Ian Gordon, Jul 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    347
    Bill Priess
    Jul 30, 2003
Loading...

Share This Page