File upload and doc type

I've made a file upload for word-files. It works fine. I also have a
send_file for getting it from the server. But there must be something
wrong with the file upload. When downloading it I can't open the file.
The same if I fetch it via ftp, so it must be something about the upload
and doc type. Any ideas?

Hi Pål,

Pål Bergström wrote:

I've made a file upload for word-files. It works fine.
I also have a send_file for getting it from the server.
But there must be something wrong with the file upload.
When downloading it I can't open the file. The same if
I fetch it via ftp, so it must be something about the upload
and doc type. Any ideas?

I assume you're talking about MS-Word? If so, are treating them as binary?
IIRC, you have to.

HTH,
Bill

I assume you're talking about MS-Word? If so, are treating them as
binary?
IIRC, you have to.

HTH,
Bill

Yes it's MS-Word. How do I make it binary?

I use this to create the file on the server:

File.open("#{RAILS_ROOT}/public/documents/#{filename}", "w+") { |f|
f.write(thefile.read) }

In the db I add the file name in a varchar-field.

File.open("blah", "wb")

Only on a windows platform though.

http://www.ruby-doc.org/core/classes/IO.html

julian wrote:

File.open("blah", "wb")

Only on a windows platform though.

http://www.ruby-doc.org/core/classes/IO.html

On Jul 18, 2:42�pm, P�l Bergstr�m <rails-mailing-l...@andreas-s.net>

Then "w+" should be ok I guess. What about this

{ |f| f.write(thefile.read) }

Is there something I do wrong there, that somehow makes it into
something different than MS-Word?

Hi Pål,

Pål Bergström wrote:

julian wrote:
> File.open("blah", "wb")
>
> Only on a windows platform though.
>
> http://www.ruby-doc.org/core/classes/IO.html
>
Then "w+" should be ok I guess. What about this

Did you try setting it to "wb"? I've never been clear about whether that
"Windows-only" comment in the documentation referred to the server or the
client. At any rate, I know from experience that using it doesn't hurt
because I develop on Windows and deploy to Linux and had to use it to handle
PDF's.

{ |f| f.write(thefile.read) }

Is there something I do wrong there, that somehow
makes it into something different than MS-Word?

A couple of other thing occured to me that might be causing you problems.
One is whether or not Word files have to be treated as multipart. The
second is the difference between IO streams and tempfiles. Which one you
get depends on the size of the file being uploaded and you have to handle
them differently. Here's a snippet from some working code that uploads PDF
files...

In the view:

<%= start_form_tag({:controller => 'transfer', :action => 'read_in'},
                    :method => "post", :multipart => true) %>
      <div class='Entryline'>
        <label>File to Upload:&nbsp;</label>
        <%= file_field_tag "file_to_upload", :size => 50 %>
      </div>
      <div>
          <%= submit_tag value="Import file", :class => 'submit-btn',
:disable_with => "Please Wait" %>
     </div>
<%= end_form_tag %>

In the read_in method in the controller:

if params[:file_to_upload].instance_of?(Tempfile)
   FileUtils.copy(params[:file_to_upload].local_path,
"#{RAILS_ROOT}/private/#{@filenametouse}")
else
  File.open("#{RAILS_ROOT}/private/#{@filenametouse}","wb"){|f|
                   f.write(params[:file_to_upload].read)
                   f.close}
end

HTH,
Bill

Bill Walton wrote:

if params[:file_to_upload].instance_of?(Tempfile)
   FileUtils.copy(params[:file_to_upload].local_path,
"#{RAILS_ROOT}/private/#{@filenametouse}")
else
  File.open("#{RAILS_ROOT}/private/#{@filenametouse}","wb"){|f|
                   f.write(params[:file_to_upload].read)
                   f.close}
end

HTH,
Bill

Thank you. I'll give it a try.

Pål Bergström wrote:

Bill Walton wrote:

if params[:file_to_upload].instance_of?(Tempfile)
   FileUtils.copy(params[:file_to_upload].local_path,
"#{RAILS_ROOT}/private/#{@filenametouse}")
else
  File.open("#{RAILS_ROOT}/private/#{@filenametouse}","wb"){|f|
                   f.write(params[:file_to_upload].read)
                   f.close}
end

HTH,
Bill

Thank you. I'll give it a try.

Didn't work. The only diff is that I use w+ instead because it's a Mac
OS server running Litespeed.

Hmm. Strange. Same problem with .rtf-files. Can it be something about
the encoding?

If nothing else I'll have to solve it with php, but that would be very
disappointing. Sourly it must be possible with Ruby / Rails to send what
ever file you want to the server.

Sorry, but how hard can it be? Can Rails handle a file upload or not? I
mean other than images and make them accessible by download.

Not hard at all, apparently. :slight_smile:

Since I'd never done this in Rails and knew I'll need it soon, I just
created a new project (Rails 2.1), installed the attachment_fu plugin
and, following this handy-dandy tutorial:
  <http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu>
:: created an upload tester in about 15 minutes.

I uploaded a Word (97, .doc) file and a PDF file. And then opened the
stored files successfully.

So I think it's a given that Rails (or at least attachment_fu) can handle
whatever binary file type you have. :slight_smile:

Have you compared checksums of the two versions of the file you're
having trouble with? Are you *sure* you downloaded it using FTP
*in binary mode*?

Hassan Schroeder wrote:

On Sat, Jul 19, 2008 at 12:57 PM,

I uploaded a Word (97, .doc) file and a PDF file. And then opened the
stored files successfully.

So I think it's a given that Rails (or at least attachment_fu) can
handle
whatever binary file type you have. :slight_smile:

Have you compared checksums of the two versions of the file you're
having trouble with? Are you *sure* you downloaded it using FTP
*in binary mode*?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

If attachment_fu works then it should work. I would like to learn it
myself without any plugin. I believe I will learn more from that, and to
be more free to what I want to do.

It's binary.

The problem is uploading. No problem downloading a "good" file that's
been uploaded by ftp.

OK, so there's something wrong with your upload code -- you might
be able to get some insight by looking at how attachment_fu does it,
but whatever.

As I said, comparing checksums would be my first move. Maybe try a
couple of small text files, too, just for comparison.

Good luck,

Hassan Schroeder wrote:

if params[:file_to_upload].instance_of?(Tempfile)
   FileUtils.copy(params[:file_to_upload].local_path,
"#{RAILS_ROOT}/private/#{@filenametouse}")
else
  File.open("#{RAILS_ROOT}/private/#{@filenametouse}","wb"){|f|
                   f.write(params[:file_to_upload].read)
                   f.close}

Can't get .local_path to work. Is that a rails method or something from
Ruby?