# Downloading files from database

**URL:** <https://www.ruby-forum.com/t/downloading-files-from-database/73560>\
**Category:** Rails\
**Created:** [October 25, 2006, 9:51am UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560 "2006-10-25T09:51:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamonduty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5fc32e/32.png) [@adamonduty](https://www.ruby-forum.com/u/adamonduty)\
**Post date:** [October 25, 2006, 9:51am UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560/1 "2006-10-25T09:51:15Z")

</div>

hallo everybody,

i got a weird problem with some code i wrote. it’s a function to save  
files to the database and later download them again.

the weird thing is, that on my home machine everything is working  
correctly, but on my webserver some (not all, only some!) files get  
corrupted when i try to download them. that means my browser shows me a  
filesize of e.g. 1,3mb but can only download 1,1mb and then aborts the  
download. --\> the file gets corrupted!

here is my code:

```auto

  def new
    @download = Download.new
    if request.post?
      @download = Download.new
      @download.date = Time.now
      @download.attributes = @params[:download]
      unless @params[:chosen_file] == ""
        @download.filename = 
@params[:chosen_file].original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
        @download.content = @params[:chosen_file].content_type
        @download.file = @params[:chosen_file].read
        @params[:download].delete(:chosen_file)
      end
      if @download.save
        flash[:notice] = 'Download wurde erfolgreich gespeichert.'
        redirect_to(:action => 'list')
      end
    end
  end

  def get
    @download = Download.find(@params[:id])
    send_data @download.file, :filename => @download.filename, :content 
=> @download.content
  end

```

has anybody got an idea what could be wrong with that? if you need more  
information, just tell me what you need to know.

i already checked if filesize could be a problem, but as i’am able to  
upload even bigger files, everything seems to be ok. i’m stuck…

thanks in advance for any suggestion  
n0ne

---

<div class="post-metadata">

**Author:** ![adamonduty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5fc32e/32.png) [@adamonduty](https://www.ruby-forum.com/u/adamonduty)\
**Post date:** [October 25, 2006, 12:39pm UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560/2 "2006-10-25T12:39:20Z")

</div>

What deployment architecture are you using ? Apache 1.3.x + mod fast cgi  
defaults to timing out actions after 30 seconds I believe, could it be  
something like that ?

Fred

---

<div class="post-metadata">

**Author:** ![adamonduty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5fc32e/32.png) [@adamonduty](https://www.ruby-forum.com/u/adamonduty)\
**Post date:** [October 25, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560/3 "2006-10-25T17:48:01Z")

</div>

has anybody got an idea what could be wrong with that? if you need more

> information, just tell me what you need to know.
> 
> i already checked if filesize could be a problem, but as i’am able to  
> upload even bigger files, everything seems to be ok. i’m stuck…
> 
> thanks in advance for any suggestion  
> n0ne

Look for any differences in the environment. Are you using the same  
brower?  
Is it the same version etc? Maybe you are not setting the content type  
in  
your code.

---

<div class="post-metadata">

**Author:** ![adamonduty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5fc32e/32.png) [@adamonduty](https://www.ruby-forum.com/u/adamonduty)\
**Post date:** [October 28, 2006, 8:17pm UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560/4 "2006-10-28T20:17:47Z")

</div>

Bala P. wrote:

> Look for any differences in the environment. Are you using the same  
> brower?  
> Is it the same version etc? Maybe you are not setting the content type  
> in  
> your code.

i’m using the same browser. even tried it with different ones (opera,  
firefox, konqueror, internetexplorer).

and i think i set the content type correctly.

n0ne wrote:

> @download.content = @params[:chosen\_file].content\_type

otherwise the rest of the downloads would not be working correctly.  
apparently this error only occurrs with some files. it can be reproduced  
with these files, but it won’t happen to others.

any other ideas anyone?  
could it be a problem with the webserver?

---

<div class="post-metadata">

**Author:** ![adamonduty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5fc32e/32.png) [@adamonduty](https://www.ruby-forum.com/u/adamonduty)\
**Post date:** [October 28, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560/5 "2006-10-28T20:11:47Z")

</div>

Frederick C. wrote:

> What deployment architecture are you using ? Apache 1.3.x + mod fast cgi  
> defaults to timing out actions after 30 seconds I believe, could it be  
> something like that ?

i’m currently using

Debian Sarge  
Rails 1.1.4  
Litespeed 2.2  
MySQL 4.1.11

but nevertheless i don’t hink it could be some timeout, because the  
download starts and is active for some time, stops (=fails) and cannot  
be resumed to complete the file. of course i can restart it but it would  
crash at the same point every time.  
on the other hand i am able to download bigger files, too. while the pdf  
i mentioned (1.3mb) stops at 1.1mb, other files (even other pdf’s) that  
have a filesize bigger than that (even \> 10mb) are working fine.

---

<div class="post-metadata">

**Author:** ![adamonduty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5fc32e/32.png) [@adamonduty](https://www.ruby-forum.com/u/adamonduty)\
**Post date:** [October 28, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560/6 "2006-10-28T21:03:50Z")

</div>

When faced with problems with many possible directions, you must prod  
the environment - using the results of the prod to indicate the next  
area for inspection.

In my opinion, you should first determine whether its a server or  
client issue.

To test this I’d write a functional test - download many different  
files many times. The results of this test will suggest where to look  
next. (a browser issue, or server, or potentially a specific file issue)

This approach enables your debugging by simplifying the environment.

and then you’ll find Sherlock Holmes to be especially useful in  
determining your next course of action:  
“that when you have eliminated the impossible, whatever remains,  
however improbable, must be the truth”  
Good luck.  
Jodi

---

<div class="post-metadata">

**Author:** ![adamonduty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5fc32e/32.png) [@adamonduty](https://www.ruby-forum.com/u/adamonduty)\
**Post date:** [November 30, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560/7 "2006-11-30T13:53:45Z")

</div>

The issue is resolved now.  
The developers from litespeed helped in finding a configuration option  
that had to be adjust in order to make this work again.

Feurio

---

<div class="post-metadata">

**Author:** ![adamonduty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5fc32e/32.png) [@adamonduty](https://www.ruby-forum.com/u/adamonduty)\
**Post date:** [November 27, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/downloading-files-from-database/73560/8 "2006-11-27T19:56:19Z")

</div>

Hello,

we did narrow the problem area:

1. We could save the problem files via console and open it them. The  
file contents were ok. -\> So it seems not to be the database.
2. We started the application with webbrick in prod mode. Everything  
worked. All downloaded files are ok. -\> So it seems the ruby code for  
file up- and download is OK, too.

This leaves the webserver for me. We use Litespeed 2.2 and tried to  
post the topic into the support forum of Litespeed Ltd., but I was not  
able to do it, because I couldn´t register for the forum.

Feurio  
I hope someone else from the rails list can help and has some tips or  
advices.
