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)
http.read_timeout=3600
req.start do |request, response|
#puts "body: #{response.body}"
end
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
req.body_stream
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
set_upload_progress_callback
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