Jump to content

Classic ASP File Headers

- - - - -

  • Please log in to reply
4 replies to this topic

#1
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
I'm trying to modify the headers so that I can download attachments. However, when I set the "filename" paramenter it just seems to be ignored. I attempt to download it and the filename is the same as the file I am on "download.asp" which is not what I want. I've even tried hard coding this in.

I'm probably missing something rather stupid, so I thought I'd ask.
        ' Content Type
            Response.ContentType = attachHandle("type")
        ' Write my file out    
            Response.AddHeader "Content-disposition: attachment;","filename=image.gif"
            'Response.AddHeader "Content-Disposition: attachment;","filename=" & attachHandle("filename") & ";"
        ' Send attachment's contents to the client
            Response.BinaryWrite attachHandle("data")

The file downloads just fine it just seems the filename is giving me a problem. Thanks in advance.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,822 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Odd note: one of your lines has a semicolon after the filename, one doesn't.

How do I prompt a "Save As" dialog for an accepted mime type?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Thanks WP,

I'm still getting use to this ASP Stuff. I did get it working without ADO Stream, I posted that at the bottom.

The ADO kept giving me errors when I tried reading it. I may be using the wrong functions I can't seem to find any examples that don't use loadFromFile. I saw one that did "WriteText" and "ReadText" I just used w3 schools to find the binary versions "Write/Read" and then included Response.BinaryWrite.

For learning purposes I'd still probably like to get this one working.

        ' Print out my file using ADO Stream
            Set oStream = Server.CreateObject("ADODB.Stream")
            oStream.Open()
                oStream.Type = 1
                oStream.Position = 0
                oStream.Write(attachHandle("data")) ' Data contains the binary of the file
                Response.ContentType = attachHandle("type") ' Content Type
                Response.AddHeader "Content-Disposition:","attachment; filename=" & attachHandle("filename")&";"
                Response.BinaryWrite oStream.Read() ' Gives me a 404 if this line is uncommented
            oStream.Close
            Set oStream = Nothing
The original one was probably just messing up because of the headers since this now works:
            Response.ContentType = attachHandle("type")
            Response.AddHeader "Content-Disposition:","attachment; filename=" & attachHandle("filename")&";"
            Response.BinaryWrite attachHandle("data")
The semicolon thing is weird. I know in PHP you don't "need" a semicolon for the last statement although I'd never not include it for any reason it just makes it cleaner. This seems to be the same case for headers since it's not outputting an error or anything. I'm not sure how it would though. Most examples don't include it, in my first post I just added it so it looked more uniform.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,822 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
My opinion, based on experience: ADO sucks. You may want to look at using a filestream.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
You could try this code snippet that works for most of the Mime Types,
if (!string.IsNullOrEmpty(strFileName))
            {
                string strDiskPath = GetFullPath(FolderPath, strFileName);//GetFullPath is custom method that i  wrote to get FullPath
                //Send the File to the Client
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
                Response.WriteFile(strDiskPath);
                Response.Flush();
                Response.End();
            }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users