# File not uploading properly

**URL:** <https://www.ruby-forum.com/t/file-not-uploading-properly/178292>\
**Category:** Rails\
**Created:** [November 2, 2009, 9:43am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292 "2009-11-02T09:43:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 2, 2009, 9:43am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/1 "2009-11-02T09:43:14Z")

</div>

Hi all,

I am using the following code to upload a file.

views:  
\<%= file\_field “upload”, ‘file’ ,:size=\> ‘30’ %\>

controller:  
file\_path=  
“#{RAILS\_ROOT}/public/email\_attachments/#{params[:upload][:file].original\_filename}”  
file = File.new(File.join(file\_path), “wb”)  
file.write(params[‘file\_’ + i.to\_s].read)

Next I am attaching the uploaded file and sending an email. Everything  
is working fine.

BUT, If I attach a big size file or Image, the file will not be uploaded  
completely. For Example If I upload a big image, I ll be able to see  
only half of the image, can anyone tell me the reason and solution to  
this problem.

Thanks in advance,  
Raju

---

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 2, 2009, 10:50am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/2 "2009-11-02T10:50:27Z")

</div>

On Mon, Nov 2, 2009 at 9:43 AM, Raju A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “#{RAILS\_ROOT}/public/email\_attachments/#{params[:upload][:file].original\_filename}”  
> file = File.new(File.join(file\_path), “wb”)  
> file.write(params[‘file\_’ + i.to\_s].read)
> 
> Next I am attaching the uploaded file and sending an email. Everything  
> is working fine.
> 
> BUT, If I attach a big size file or Image,  
> What exactly is a big size file? I mean, starting from what size is  
> considered big?

> the file will not be uploaded  
> completely. For Example If I upload a big image, I ll be able to see  
> only half of the image, can anyone tell me the reason and solution to  
> this problem.

Maybe you’re getting some timeout error?  
Maybe you can try some plugin for file upload such as attachment\_fu or  
paperclip or anyone you like.  
Is there any reason why you’re not using some of this and doing the  
file creation by hand?

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 2, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/3 "2009-11-02T13:18:33Z")

</div>

which server are you using … you can increase maximum file upload  
size and  
maximum execution time as well … hope this may solve the problem

On Nov 2, 3:43 pm, Raju A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 3, 2009, 6:50am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/4 "2009-11-03T06:50:41Z")

</div>

Thanks a lot for your reply,

I have used multiple file upload here is the view part.

\<%= file\_field “upload”, ‘file’ ,:size=\> ‘30’, :style=\>  
‘color:red;’%\>

Attached Files

and javascript.

when it goes to the action I will be getting the files attached in  
params[:file\_1], params[:file\_2]… etc. params[“file\_nos”] is number  
of files being uploaded and thus it should loop that many times.

The controller goes like this:

params[“file\_nos”].to\_i.times do |i|  
if params[‘file\_’+i.to\_s].to\_s != “” || params[‘file\_’+i.to\_s] !=  
nil  
#begin  
@email\_attachment = SupportEmailAttachment.new  
@email\_attachment.filename = params[‘file\_’ +  
i.to\_s].original\_filename  
@email\_attachment.save  
file\_path=  
“#{RAILS\_ROOT}/public/email\_attachments/#{params[‘file\_’ +  
i.to\_s].original\_filename}”  
file = File.new(File.join(file\_path), “wb”)  
file.write(params[‘file\_’ + i.to\_s].read)  
file\_path\_array \<\< file\_path  
#rescue  
#end  
end  
end  
Mailer.deliver\_create\_ticket user\_email,file\_path\_array

After the files are uploaded I am attaching all the files in the mail  
and sending them.

So here is my problem,

1. The files attached in the mails are not complete. ie if i have upload  
any file, there will be all attachments but they wont open, some images  
uploaded will display only half.

I am using apache + mongrel congiguration.

Please help me its urgent.

Thanks,  
Raju

---

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 3, 2009, 10:30am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/5 "2009-11-03T10:30:19Z")

</div>

Thanks for your reply fred,

file = File.new(File.join(file\_path), “wb”)  
file.write(params[‘file\_’ + i.to\_s].read)

You mean to say my code must be like this,

file = File.new(File.join(file\_path), “wb”)  
file.write(params[‘file\_’ + i.to\_s].read)  
file.close

But I am not getting the attachments properly in my email. If you can  
send your email ID I will send an email to you through my app, so that  
you can tell me what is the problem

Thanks,  
Raju

---

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 3, 2009, 9:25am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/6 "2009-11-03T09:25:56Z")

</div>

On Nov 3, 5:50 am, Raju A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> file = File.new(File.join(file_path), "wb")
> file.write(params['file_' + i.to_s].read)
> 
> ```

You’re not closing the file you create, which could conceivably cause  
problems (if you use the block form of File.open you don’t need to  
worry about that)

Fred

---

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 3, 2009, 11:10am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/7 "2009-11-03T11:10:24Z")

</div>

In ur form tab write this property =\>  
enctype=“multipart/form-data”

---

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 3, 2009, 11:39am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/8 "2009-11-03T11:39:52Z")

</div>

Thanks for your help,

Its working fine now. I used attachment\_fu plugin.

Thanks,  
Raju

---

<div class="post-metadata">

**Author:** ![raju\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@raju\_k](https://www.ruby-forum.com/u/raju_k)\
**Post date:** [November 3, 2009, 10:58am UTC](https://www.ruby-forum.com/t/file-not-uploading-properly/178292/9 "2009-11-03T10:58:23Z")

</div>

On Nov 3, 9:30 am, Raju A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

yup, something like that.

File.open(path, ‘wb’) do |file|  
file.write(…)  
end

is nicer because it ensures the file gets closed (obviously you could  
replicate this yourself but File.open already does it so not much  
point)

> But I am not getting the attachments properly in my email. If you can  
> send your email ID I will send an email to you through my app, so that  
> you can tell me what is the problem

Sounds like you need to isolate the problem: is the mailer fine but it  
is being given half uploaded files, or is the uploading part happening  
file but the mailer is ok?

Fred
