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

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

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!
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.
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
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!
No idea. Please add a separate question for that and try to explain your problem in more detail. It is a completely different topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.