Difference between revisions of "Ns returnfile"
Jump to navigation
Jump to search
Jadeforrest (talk | contribs) |
|||
(12 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
+ | ns_returnfile 200 "application/zip" $filepath | ||
+ | ns_returnfile 200 [ns_guesstype $file] $filepath | ||
− | + | ==== Caveat programmor ==== | |
− | |||
− | For | + | ns_returnfile uses [[fastpath]] caching internally, and fastpath caching may fail to distinguish between two files on the same filesystem which have the same inode, mtime, and size (serving one file in lieu of the other). This can happen if a file is generated, returned to the user, and then deleted. For example, in the following sample code the second call to ns_returnfile will return /var/tmp/myfile rather than /var/tmp/myotherfile: |
+ | |||
+ | set file [open "/var/tmp/myfile" "w"] | ||
+ | puts $file "ABC123" | ||
+ | close $file | ||
+ | ns_returnfile 200 text/plain "/var/tmp/myfile" | ||
+ | ns_unlink -nocomplain "/var/tmp/myfile" | ||
+ | |||
+ | set file [open "/var/tmp/myotherfile" "w"] | ||
+ | puts $file "XYZ987" | ||
+ | close $file | ||
+ | ns_returnfile 200 text/plain "/var/tmp/myotherfile" | ||
+ | ns_unlink -nocomplain "/var/tmp/myotherfile" | ||
+ | |||
+ | When returning "dynamic" file data like this, a safer practice would be to use [[ns_return]] or ns_returnfp instead (since they don't use fastpath caching). For example: | ||
+ | |||
+ | set fd [open $myfile] | ||
+ | ns_return 200 [ns_guesstype $myfile] [read $fd] | ||
+ | close $fd | ||
+ | |||
+ | ==== Want to name the file returned as well? ==== | ||
+ | |||
+ | ns_set update [ns_conn outputheaders] content-disposition "attachment; filename=1.abc" | ||
+ | |||
+ | [[Category:Documentation]] - [[Category:Core Tcl API]] |
Latest revision as of 19:26, 7 February 2009
ns_returnfile 200 "application/zip" $filepath ns_returnfile 200 [ns_guesstype $file] $filepath
Caveat programmor
ns_returnfile uses fastpath caching internally, and fastpath caching may fail to distinguish between two files on the same filesystem which have the same inode, mtime, and size (serving one file in lieu of the other). This can happen if a file is generated, returned to the user, and then deleted. For example, in the following sample code the second call to ns_returnfile will return /var/tmp/myfile rather than /var/tmp/myotherfile:
set file [open "/var/tmp/myfile" "w"] puts $file "ABC123" close $file ns_returnfile 200 text/plain "/var/tmp/myfile" ns_unlink -nocomplain "/var/tmp/myfile" set file [open "/var/tmp/myotherfile" "w"] puts $file "XYZ987" close $file ns_returnfile 200 text/plain "/var/tmp/myotherfile" ns_unlink -nocomplain "/var/tmp/myotherfile"
When returning "dynamic" file data like this, a safer practice would be to use ns_return or ns_returnfp instead (since they don't use fastpath caching). For example:
set fd [open $myfile] ns_return 200 [ns_guesstype $myfile] [read $fd] close $fd
Want to name the file returned as well?
ns_set update [ns_conn outputheaders] content-disposition "attachment; filename=1.abc" -