NetTalk Central

Author Topic: Downloading any file and forcing the browser to show "save file"  (Read 20899 times)

random69

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Downloading any file and forcing the browser to show "save file"
« on: December 05, 2008, 09:20:00 AM »
Just wanted to share this. Basically wanted a way to server up ANY file and have the user save/open it.

Basically, you have to send a different header than what NT sends.

Create a new web page, and set the header to other. Uncheck Send Header.

Then put the code below under Processed Code, Priority 500 (must be 500, before calling the header routine).

Code: [Select]
   ! set our content type
   lCT = 'application/x-download'

   ! send custom header
   packet = 'HTTP/1.0 200 OK<13,10>' & |
            'Content-Type: ' & CLIP(lCT) & '<13,10>' & |
            'Content-Disposition: attachment; filename=' & CLIP(lFN) & '<13,10>' & |
            '<13,10>'

   do sendpacket


   ! send it
   p_web._SendFile(lFileName,NET:NoHeader)

   ! return without footer
   return

Obviously, you have to setup lFilename as the full filename to download - remember security issues too. (I get the filename using GetValue, validate the login and location of the file is ok).

lCT is the content type - set to x-download
lFN is the filename only

FYI - The 'content-disposition' header tag is what causes the dialog box to show up on the browser, allowing the user to save or open the file.

You could also modify the content type and remove the disposition based on the extension of the file.

You might want to add other header tags to deal with caching, dates, etc - but this appears to work for me at this time the way it is.


BRUCE: It would be VERY COOL to setup utility functions to change/add header tags before sending the header out. Take a look at the header() function within PHP.
Also - unchecking the "Send HTTP Header" doesn't work - that's why the code above has to be at priority 500.

Hope this helps to other people....

Cheers!

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Downloading any file and forcing the browser to show "save file"
« Reply #1 on: December 05, 2008, 09:34:47 PM »
Hi

yes, I think you'll find an example of this in the FileDownload example.

cheers
Bruce

random69

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: Downloading any file and forcing the browser to show "save file"
« Reply #2 on: December 11, 2008, 01:07:27 PM »
Hi Bruce,

I'm on 4.29 - but apparently you are aware of this already based on other topics on content-disposition. I won't upgrade until things stabilize anyhow.

Did you include a way to change content-disposition in later builds? For that matter - any way of changing the actual header?

Thanks.

Dave



Hi

yes, I think you'll find an example of this in the FileDownload example.

cheers
Bruce

« Last Edit: December 11, 2008, 01:12:23 PM by random69 »

npa

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Downloading any file and forcing the browser to show "save file"
« Reply #3 on: December 12, 2008, 08:23:39 AM »
I was wondering guys, how would you do the type change to Other in a Net web Form...?

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Downloading any file and forcing the browser to show "save file"
« Reply #4 on: December 14, 2008, 10:11:52 PM »
Hi Npa,

you wouldn't need to I don't think because
a) I'm not sure why you'd want the user to "save" the netwebform rather than view it and
b) you don't need to set the content-type to set the content-disposition.

Cheers
Bruce

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Downloading any file and forcing the browser to show "save file"
« Reply #5 on: December 14, 2008, 10:57:06 PM »
Hi Dave,

>> Did you include a way to change content-disposition in later builds? For that matter - any way of changing the actual header?

You've got more-or-less complete control of the header.

There's a property of the web handler class called HeaderDetails (Group).
In this property are a bunch of strings -
HTTP                                    string(2048) ! e.g HTTP/1.0
ResponseNumber                          string(3)  ! e.g. 200
_Spacer1                                string(1)  !
ResponseString                          string(80) ! e.g. OK
Date                                    long       ! Clarion Date Long
Time                                    long       ! Clarion Time Long
ExpiresDate                             long       ! Clarion Date Long
ExpiresTime                             long       ! Clarion Time Long
LastModifiedDate                        long       ! Clarion Date Long
LastModifiedTime                        long       ! Clarion Time Long
ETag                                    string(80) ! e.g. "8dc33a162a5c41:e8c"
Age                                     string(40) ! e.g 6374 (age in seconds). number between 0 and 2147483648
ContentLength                           string(40) ! bytes in the Body (not including the header)
ContentType                             string(80) ! e.g. text/html; charset=ISO-8859-4 or text/xml ! was 40
CacheControl                            string(256)! e.g. No-Cache or <blank> or max-age=900
_Pragma                                 string(40) !
Server                                  string(40) ! Server Description e.g. Apache or Microsoft-IIS/5.0 etc.
Location                                string(1024)
WWWAuthenticate                         string(80)
Cookies                                 String(2048)
Connection                              string(40) ! e.g. close
ContentEncoding                         string(40) ! e.g. gzip
ContentDisposition                      string(256) ! e.g. 'Content-Disposition: attachment; filename="employees.csv"

Thus, in the handler you can override pretty much any of the header contents at more or less any time. (There are some specific exceptions - where the classes specifically set header options, but then all you need to do is set it _after_ the class does.)

Cheers
Bruce