# Send file from CGI

**URL:** <https://www.ruby-forum.com/t/send-file-from-cgi/119185>\
**Category:** Ruby\
**Created:** [November 4, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/send-file-from-cgi/119185 "2007-11-04T17:29:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [November 4, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/send-file-from-cgi/119185/1 "2007-11-04T17:29:40Z")

</div>

Hi,

in a webapplication, a user can select from some options. Depending on  
the selection some files are packed together in a zip file and get send  
to the user. So far it works perfect, but the zip file is not recognized  
from the browser and it displays the data as garbage.

The code that sends the data:

print @cgi.header(“type” =\> “application/zip”)  
print @cgi.header(“length” =\> File.size(zipfile).to\_s)

file = File.new(zipfile, “r” )  
while true  
begin  
data = file.sysread(65536)  
rescue EOFError  
break  
else  
@cgi.print data  
end  
end

Any suggestions?

Cheers  
detlef

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [November 4, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/send-file-from-cgi/119185/2 "2007-11-04T17:36:06Z")

</div>

Detlef R. schrieb:

Hi Detlef,

> Any suggestions?

Wouldn’t be easier to redirect the clients browser to the zip file? In  
addition is “application/zip” the correct mime type?

HTH,

Daniel

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [November 4, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/send-file-from-cgi/119185/3 "2007-11-04T17:41:22Z")

</div>

Am Montag, den 05.11.2007, 01:34 +0900 schrieb Daniel VÃ¶lkerts:

> Detlef R. schrieb:
> 
> Hi Detlef,
> 
> > Any suggestions?
> 
> Wouldn’t be easier to redirect the clients browser to the zip file?

Hi Daniel

the zip file is not in the web root and also should not be

> In  
> addition is “application/zip” the correct mime type?

afaik yes, but i also testet with other file types, with the same  
result.

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [November 7, 2007, 1:35pm UTC](https://www.ruby-forum.com/t/send-file-from-cgi/119185/4 "2007-11-07T13:35:02Z")

</div>

Detlef R. wrote:

> the zip file is not in the web root and also should not be

why not? securing data this way is kinda stupid.

> > In  
> > addition is “application/zip” the correct mime type?
> 
> afaik yes, but i also testet with other file types, with the same  
> result.

acording to [Media Types](http://www.iana.org/assignments/media-types/) it’s.

but some shity browsers (read IE wants file to have .zip extension even  
if it’s something like bleh.html?file=bloh.zip)
