File not uploading properly

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

On Mon, Nov 2, 2009 at 9:43 AM, Raju A.
[email protected] 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 ~

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. [email protected]

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

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

On Nov 3, 5:50 am, Raju A. [email protected]
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

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

Thanks for your help,

Its working fine now. I used attachment_fu plugin.

Thanks,
Raju

On Nov 3, 9:30 am, Raju A. [email protected]
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