Downloading files of known mime type

Discussion in 'ASP General' started by Bret Thompson, Jul 8, 2005.

  1. I am attempting to write an ASP page that will download a file rather then
    open it, when it is of known type. I found this code here:
    http://mosley.arach.net.au/dev/docs/save as.htm

    The first example works fine, for files under 5 Mb, the second example
    downloads the file but the data is corrupted. Using a JGP file as a
    reference, it seems to corrupt the end of the file (since the bottom of the
    JPEG is corrupted).

    Here is the code I am using. Any idea how to fix this? or an alternative
    method?


    Response.ContentType = "application/x-unknown"
    Response.AddHeader "content-disposition","attachment; filename=" &
    Request.QueryString("File")

    Set adoStream = CreateObject("ADODB.Stream")
    chunk = 2048
    adoStream.Open()
    adoStream.Type = 1
    adoStream.LoadFromFile(Server.MapPath(vFilePath))

    iSz = adoStream.Size

    Response.AddHeader "Content-Length", iSz

    For i = 1 To iSz \ chunk
    If Not Response.IsClientConnected Then Exit For
    Response.BinaryWrite adoStream.Read(chunk)
    Next

    If iSz Mod chunk > 0 Then
    If Response.IsClientConnected Then
    Response.BinaryWrite objStream.Read(iSz Mod chunk)
    End If
    End If

    adoStream.Close
    Set adoStream = Nothing

    Response.End
    Bret Thompson, Jul 8, 2005
    #1
    1. Advertising

  2. What's with the chunks? Perhaps try this code here.

    http://www.aspfaq.com/show.asp?id=2161

    Ray at work

    "Bret Thompson" <Bret > wrote in message
    news:...
    > I am attempting to write an ASP page that will download a file rather then
    > open it, when it is of known type. I found this code here:
    > http://mosley.arach.net.au/dev/docs/save as.htm
    >
    > The first example works fine, for files under 5 Mb, the second example
    > downloads the file but the data is corrupted. Using a JGP file as a
    > reference, it seems to corrupt the end of the file (since the bottom of

    the
    > JPEG is corrupted).
    >
    > Here is the code I am using. Any idea how to fix this? or an alternative
    > method?
    >
    >
    > Response.ContentType = "application/x-unknown"
    > Response.AddHeader "content-disposition","attachment; filename=" &
    > Request.QueryString("File")
    >
    > Set adoStream = CreateObject("ADODB.Stream")
    > chunk = 2048
    > adoStream.Open()
    > adoStream.Type = 1
    > adoStream.LoadFromFile(Server.MapPath(vFilePath))
    >
    > iSz = adoStream.Size
    >
    > Response.AddHeader "Content-Length", iSz
    >
    > For i = 1 To iSz \ chunk
    > If Not Response.IsClientConnected Then Exit For
    > Response.BinaryWrite adoStream.Read(chunk)
    > Next
    >
    > If iSz Mod chunk > 0 Then
    > If Response.IsClientConnected Then
    > Response.BinaryWrite objStream.Read(iSz Mod chunk)
    > End If
    > End If
    >
    > adoStream.Close
    > Set adoStream = Nothing
    >
    > Response.End
    >
    Ray Costanzo [MVP], Jul 8, 2005
    #2
    1. Advertising

  3. Ray,

    Your link is the exact same code at the correct site. Scroll down to the
    part that says
    "Many people have complained that larger files simply do not work."
    And you will see where I got the code with the "Chunks". This is thier
    solution for large files.

    The first code example fails with files larger then roughly 5 Mb.

    Bret


    "Ray Costanzo [MVP]" wrote:

    > What's with the chunks? Perhaps try this code here.
    >
    > http://www.aspfaq.com/show.asp?id=2161
    >
    > Ray at work
    >
    > "Bret Thompson" <Bret > wrote in message
    > news:...
    > > I am attempting to write an ASP page that will download a file rather then
    > > open it, when it is of known type. I found this code here:
    > > http://mosley.arach.net.au/dev/docs/save as.htm
    > >
    > > The first example works fine, for files under 5 Mb, the second example
    > > downloads the file but the data is corrupted. Using a JGP file as a
    > > reference, it seems to corrupt the end of the file (since the bottom of

    > the
    > > JPEG is corrupted).
    > >
    > > Here is the code I am using. Any idea how to fix this? or an alternative
    > > method?
    > >
    > >
    > > Response.ContentType = "application/x-unknown"
    > > Response.AddHeader "content-disposition","attachment; filename=" &
    > > Request.QueryString("File")
    > >
    > > Set adoStream = CreateObject("ADODB.Stream")
    > > chunk = 2048
    > > adoStream.Open()
    > > adoStream.Type = 1
    > > adoStream.LoadFromFile(Server.MapPath(vFilePath))
    > >
    > > iSz = adoStream.Size
    > >
    > > Response.AddHeader "Content-Length", iSz
    > >
    > > For i = 1 To iSz \ chunk
    > > If Not Response.IsClientConnected Then Exit For
    > > Response.BinaryWrite adoStream.Read(chunk)
    > > Next
    > >
    > > If iSz Mod chunk > 0 Then
    > > If Response.IsClientConnected Then
    > > Response.BinaryWrite objStream.Read(iSz Mod chunk)
    > > End If
    > > End If
    > >
    > > adoStream.Close
    > > Set adoStream = Nothing
    > >
    > > Response.End
    > >

    >
    >
    >
    Bret Thompson, Jul 8, 2005
    #3
  4. Ray,

    Nevermind, I finally found it. There is a typo in thier code. Look at the
    2 Response.BinaryWrite statements:
    Response.BinaryWrite adoStream.Read(chunk)
    Response.BinaryWrite objStream.Read(iSz Mod chunk)

    Bret


    "Ray Costanzo [MVP]" wrote:

    > What's with the chunks? Perhaps try this code here.
    >
    > http://www.aspfaq.com/show.asp?id=2161
    >
    > Ray at work
    >
    > "Bret Thompson" <Bret > wrote in message
    > news:...
    > > I am attempting to write an ASP page that will download a file rather then
    > > open it, when it is of known type. I found this code here:
    > > http://mosley.arach.net.au/dev/docs/save as.htm
    > >
    > > The first example works fine, for files under 5 Mb, the second example
    > > downloads the file but the data is corrupted. Using a JGP file as a
    > > reference, it seems to corrupt the end of the file (since the bottom of

    > the
    > > JPEG is corrupted).
    > >
    > > Here is the code I am using. Any idea how to fix this? or an alternative
    > > method?
    > >
    > >
    > > Response.ContentType = "application/x-unknown"
    > > Response.AddHeader "content-disposition","attachment; filename=" &
    > > Request.QueryString("File")
    > >
    > > Set adoStream = CreateObject("ADODB.Stream")
    > > chunk = 2048
    > > adoStream.Open()
    > > adoStream.Type = 1
    > > adoStream.LoadFromFile(Server.MapPath(vFilePath))
    > >
    > > iSz = adoStream.Size
    > >
    > > Response.AddHeader "Content-Length", iSz
    > >
    > > For i = 1 To iSz \ chunk
    > > If Not Response.IsClientConnected Then Exit For
    > > Response.BinaryWrite adoStream.Read(chunk)
    > > Next
    > >
    > > If iSz Mod chunk > 0 Then
    > > If Response.IsClientConnected Then
    > > Response.BinaryWrite objStream.Read(iSz Mod chunk)
    > > End If
    > > End If
    > >
    > > adoStream.Close
    > > Set adoStream = Nothing
    > >
    > > Response.End
    > >

    >
    >
    >
    Bret Thompson, Jul 8, 2005
    #4
    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. CJ
    Replies:
    1
    Views:
    1,564
    Andrew Thompson
    Oct 29, 2004
  2. mizi

    Download dialog for known MIME type

    mizi, Oct 30, 2005, in forum: ASP General
    Replies:
    0
    Views:
    162
  3. Jan Arickx
    Replies:
    0
    Views:
    192
    Jan Arickx
    Aug 25, 2003
  4. joe
    Replies:
    0
    Views:
    188
  5. ecureuil
    Replies:
    0
    Views:
    320
    ecureuil
    May 28, 2006
Loading...

Share This Page