Upload a big file using Sketchup::Http::Request


I try to upload a file using Sketchup::Http::Request class. I use the code bellow:

body << "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"" + param_name.to_s + "\"; filename=\"" + filename + "\"\r\n" + 
"Content-Type: #{@content_type}\r\n\r\n"
body << File.read(filepath)
body << "\r\n--#{boundary}--\r\n"
req = Sketchup::Http::Request.new( Constants::SiteURL, Sketchup::Http::POST)
req.body = body.join
http = Net::HTTP.new(url.host, url.port)

req.start do |request, response|
  #puts "body: #{response.body}"

Actually it works fine when I try to upload a small file, but when the file is a little big (I’ve tried for 4 mb zip file)
the file is uploaded to the cloud but it’s size is small than it should be (only 2 kb) and the zip is corrupted.
I’ve searched a little and seems the problem is connected with File.read(filepath)

I’ve found another alternative using

req = Net::HTTP::Post.new(url.path)

and it solved my problem because it has


field and I could make an upload in this way

stream = File.open(filepath,"rb")
streams << stream
parts << StreamPart.new(stream, File.size(filepath))
parts << StringPart.new( "\r\n--" + boundary + "--\r\n" )
post_stream = MultipartStream.new( parts )
req.body_stream = post_stream

But I want to use Sketchup::Http::Request because it has


method which I want to use.

So please help me to understand
how can I upload a “big” file(several mb) with Sketchup::Http::Request in a correct way



Are you trying to read a binary file in text mode (File.read without mode specified) ?

If you are not using a system other than Windows, then you need to pay attention to the "rb" parameter, because Windows corrupts binary data when it reads them in text mode.

1 Like