# Send\_file : downloaded file cannot be open after upload OK

**URL:** <https://www.ruby-forum.com/t/send-file-downloaded-file-cannot-be-open-after-upload-ok/113087>\
**Category:** Rails\
**Created:** [August 27, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/send-file-downloaded-file-cannot-be-open-after-upload-ok/113087 "2007-08-27T13:04:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 27, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/send-file-downloaded-file-cannot-be-open-after-upload-ok/113087/1 "2007-08-27T13:04:25Z")

</div>

I easily implemented the upload , using the ‘attachment\_fu’ plugin…  
I can see and open the uploaded files (a pdf file 60k and a jpeg images  
28k)

now I try to implement the download action in my document\_controller, I  
wrote :

def download  
@document = Document.find(params[:id])  
send\_file(@document.full\_filename,  
:filename =\> URI.encode(@document.filename),  
:type =\> @document.content\_type,  
:encoding =\> ‘utf8’,  
:disposition =\> ‘attachment’,  
:stream =\> ‘true’,  
:buffer\_size =\> ‘4096’)  
redirect\_back\_or\_default(home\_url)  
end

I got back the downloaded files… but i always get back a 16k whatever  
it’s pdf or jpeg, and when I try to open them, I got an error :

Failed to find PDF header: `%PDF’ not found. for the pdf file and  
corrupted or not recognized…

what’s wrong in my send\_file options

note : I am testing locally on localhost

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 27, 2007, 1:12pm UTC](https://www.ruby-forum.com/t/send-file-downloaded-file-cannot-be-open-after-upload-ok/113087/2 "2007-08-27T13:12:53Z")

</div>

On 27 Aug 2007, at 13:04, Kad K. wrote:

> ```
> @document = Document.find(params[:id])
> send_file(@document.full_filename,
> :filename => URI.encode(@document.filename),
> :type => @document.content_type,
> :encoding => 'utf8',
> :disposition => 'attachment',
> :stream => 'true',
> :buffer_size => '4096')
> redirect_back_or_default(home_url)
> 
> ```
> 
> end

Don’t redirect at the end of your for one. No new page will be  
rendered anyway (the user will just get the download dialog).

Also don’t make things more complicated than they need to be:  
def download  
@document = Document.find(params[:id])  
send\_file(@document.full\_filename,  
:filename =\> @document.filename,  
:type =\> @document.content\_type,  
:disposition =\> ‘attachment’)  
end

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [August 27, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/send-file-downloaded-file-cannot-be-open-after-upload-ok/113087/3 "2007-08-27T13:39:40Z")

</div>

Peter De Berdt wrote:

> On 27 Aug 2007, at 13:04, Kad K. wrote:
> 
> > ```
> > @document = Document.find(params[:id])
> > send_file(@document.full_filename,
> > :filename => URI.encode(@document.filename),
> > :type => @document.content_type,
> > :encoding => 'utf8',
> > :disposition => 'attachment',
> > :stream => 'true',
> > :buffer_size => '4096')
> > redirect_back_or_default(home_url)
> > 
> > ```
> > 
> > end
> 
> Don’t redirect at the end of your for one. No new page will be  
> rendered anyway (the user will just get the download dialog).
> 
> Also don’t make things more complicated than they need to be:  
> def download  
> @document = Document.find(params[:id])  
> send\_file(@document.full\_filename,  
> :filename =\> @document.filename,  
> :type =\> @document.content\_type,  
> :disposition =\> ‘attachment’)  
> end
> 
> Best regards
> 
> Peter De Berdt

Thanks Peter ! up and running well…

btw , are u from SA ? (because of the name…) if yes, be our guest for  
the coming Rugby World Cup, otherwise sorry, u will not understand that  
game… ;-))
