Pål Bergström | 18 Jul 18:29

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?
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Bill Walton | 18 Jul 20:18

Re: File upload and doc type


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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Pål Bergström | 18 Jul 20:42

Re: File upload and doc type


> 
> 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.
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

julian | 18 Jul 20:58

Re: File upload and doc type


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...@...>
wrote:
> > 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.
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
(Continue reading)

Pål Bergström | 19 Jul 15:33

Re: File upload and doc type


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...@...>

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?
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Bill Walton | 19 Jul 16:03

Re: File upload and doc type


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
(Continue reading)

Pål Bergström | 19 Jul 18:28

Re: File upload and doc type


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.
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Pål Bergström | 19 Jul 18:54

Re: File upload and doc type


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.
--

-- 
Posted via http://www.ruby-forum.com/.
(Continue reading)

Pål Bergström | 19 Jul 21:57

Re: File upload and doc type


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.
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Hassan Schroeder | 19 Jul 23:32

Re: File upload and doc type


On Sat, Jul 19, 2008 at 12:57 PM, Pål Bergström
<rails-mailing-list@...> wrote:
>
> 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. :-)

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. :-)

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@...

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
(Continue reading)

Pål Bergström | 20 Jul 00:00

Re: File upload and doc type


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. :-)
> 
> 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@...

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.
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
(Continue reading)

Hassan Schroeder | 20 Jul 00:11

Re: File upload and doc type


On Sat, Jul 19, 2008 at 3:00 PM, Pål Bergström
<rails-mailing-list@...> wrote:

> The problem is uploading.

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 ------------------------ hassan.schroeder@...

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Pål Bergström | 20 Jul 00:21

Re: File upload and doc type


Hassan Schroeder wrote:
> On Sat, Jul 19, 2008 at 3:00 PM, 
> P�l Bergstr�m<rails-mailing-list@...> wrote:
> 
>> The problem is uploading.
> 
> 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 ------------------------ hassan.schroeder@...

Got the same idea. I'm looking at it now.

Thanks for the help.
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
(Continue reading)

Pål Bergström | 20 Jul 22:09

Re: File upload and doc type


>> 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?
--

-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Gmane