0

Now,I have a need that post a file stream,not a local file.the process is:

client(file) ---> my server ----> third party Cloud Storage,the transfer is file stream. I have found this article: Ruby: How to post a file via HTTP as multipart/form-data?

require 'rest_client'
RestClient.post('http://localhost:3000/foo', 
:name_of_file_param => File.new('/path/to/file'))

you can see that the name_of_file_param is a local file,not stream.

so I want to know ,if this is file stream form the client ,what should I do

Community
  • 1
  • 1
HXH
  • 1,643
  • 4
  • 19
  • 31

1 Answers1

1

You should be able to use any IO object, including a stream, as the parameter:

RestClient.post('http://localhost:3000/foo', :name_of_file_param => my_stream)
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
  • Thanks,this works.now I can use this upload the local file and a file stream.But now I need to upload a remote file to the Cloud Storage server,so I must read the remote file to a file stream,I use `open-uri` but this still not work,It seems that the stream format is not right.so what should I do? thanks! – HXH Aug 16 '14 at 18:32
  • You should be able to write the result from open-uri to a `Tempfile` or a `StringIO`, then this should again behave like any other IO object. – Patrick Oscity Aug 16 '14 at 18:43
  • yes,you are right,I want to write the result from open-uri to a `StringIO`. but when I use the `open-uri` to transfer the remote file to file stream,the Cloud Storage return a error that the format is no t right,I think the reason is that the encoding is not `ASCII-8BIT` – HXH Aug 17 '14 at 07:43
  • If the file is from user client upload then `render text: params[:file]` will raise an error `"\xFF" from ASCII-8BIT to UTF-8` this shows that the file encoding is `ASCII-8BIT`.But the remote file `render text: web_contents ` will return messy code like `����JFIF``��C ` so this encoding is not `ASCII-8BIT` not the same as custom uoload file.Can you tell me why? thanks! – HXH Aug 17 '14 at 07:48
  • No idea. Please add a separate question for that and try to explain your problem in more detail. It is a completely different topic. – Patrick Oscity Aug 17 '14 at 08:12