# Viewing attachments inline: MIME type fix

**URL:** <https://www.ruby-forum.com/t/viewing-attachments-inline-mime-type-fix/101018>\
**Category:** Ruby-Forum\
**Created:** [August 2, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/viewing-attachments-inline-mime-type-fix/101018 "2006-08-02T21:09:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [August 2, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/viewing-attachments-inline-mime-type-fix/101018/1 "2006-08-02T21:09:26Z")

</div>

Normally RForum attachments don’t have a content type set when RForum  
sends the file to the browser, so application/octet-stream is used as a  
default and most browsers pop up a “Save as” dialogue. If you would  
prefer files to be shown inline (particularly handy for images) then the  
following achieves it.

(1) Install mime-types gem: ‘gem install mime-types’  
(2) Alter ‘app/controllers/attachment\_controller.rb’:

# Index: app/controllers/attachment\_controller.rb

— app/controllers/attachment\_controller.rb (revision 44)  
+++ app/controllers/attachment\_controller.rb (working copy)  
@@ -1,7 +1,14 @@  
+require ‘mime/types’  
+  
class AttachmentController \< ApplicationController  
def fetch

- id = @params[:id]
- send\_file Attachment.find(id).file\_path

- id = @params[:id]
- file\_path = Attachment.find(id).file\_path
- mime\_type = MIME::Types.type\_for(file\_path) ||  
‘application/octet-stream’
- 
- send\_file file\_path,
- 

```
         :type => mime_type[0].content_type,

```

- 

```
         :disposition => 'inline'

```

[Rest of the diff snipped]

Does the job for me. I’ve not submitted this as a patch / ticket as it  
introduces a new external dependency so I don’t expect it to be too  
interesting as a general feature addition, but I’m happy to do so if  
people on the list think it worthwhile.

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [August 20, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/viewing-attachments-inline-mime-type-fix/101018/2 "2006-08-20T17:32:58Z")

</div>

Hi Andrew H. :  
I do want you write,but after i uploaded a picture(.jpg), i download  
the picture,the browser just redirect to a URL some like  
“[http://localhost/attachment/3/32680.jpg](http://localhost/attachment/3/32680.jpg)” and the templete is just the  
same words. i can’t download the picture.

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [August 21, 2006, 8:27am UTC](https://www.ruby-forum.com/t/viewing-attachments-inline-mime-type-fix/101018/3 "2006-08-21T08:27:28Z")

</div>

i fixed my problem,but there is still a few question:

1. i suggest to modify  
===index attachement.rb===  
def after\_save
# Create attachment directory
begin  
Dir.mkdir(self.dir\_path)  
rescue Errno::EEXIST  
end
# Save uploaded file to attachment directory

- File.open(self.file\_path, ‘w’) do |f|

- File.open(self.file\_path, ‘wb’) do |f|  
f.write(@data)  
end  
@data = nil  
end

if do not do that ,i won’t see upload file(images,or some file else)

2.if the upload file(test.txt) contain some chinese words ,it seems the  
browser can’t decode these words

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [August 21, 2006, 1:29pm UTC](https://www.ruby-forum.com/t/viewing-attachments-inline-mime-type-fix/101018/4 "2006-08-21T13:29:08Z")

</div>

my os is windows xp.

if we upload the attachment file(e.g demo1.jpg), can we edit the  
attachment file to anthoer(e.g demo2.jpg)? it seems i can’t change it.  
current version don’t surpport it?

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [August 21, 2006, 10:08am UTC](https://www.ruby-forum.com/t/viewing-attachments-inline-mime-type-fix/101018/5 "2006-08-21T10:08:07Z")

</div>

Vincent Tsao wrote:

> - File.open(self.file\_path, ‘w’) do |f|
> 
> - File.open(self.file\_path, ‘wb’) do |f|

Well spotted; the platform I’m running RForum on doesn’t seem to care  
about ‘w’ vs ‘wb’ given that attachments are saved correctly. Yours, on  
the other hand, must. Out of interest, what operating system are you  
using?

> 2.if the upload file(test.txt) contain some chinese words ,it seems the  
> browser can’t decode these words

That’s presumably because the file ‘test.txt’ by itself contains no  
indication of the character set in which the text file is encoded. Plain  
text files, unlike HTML, have no mechanism for indicating this. I don’t  
think this has any connection with RForum. You probably need to manually  
set the encoding type (e.g. in Firefox, use the “View -\> Character  
encoding” menu), set the default encoding type system-wide (the way to  
do this depends on your operating system and the browser might ignore it  
anyway), or just load the file into a text editor that you know _will_  
display the Chinese words properly.
