mercredi 18 novembre 2015

Rails upload to SmartFile Multipart form

Hello i am trying to upload a file to SmartFile(Free 100GB Media upload for developers) using rails with Net::HTTP but it always return Unsuported Media type 415 here is the upload function

    def upload_sound(file)
  multipart = []
  multipart << file_to_multipart(file)
  uri = URI.parse(@base_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  @request = Net::HTTP::Post.new(uri.request_uri)
  @request.basic_auth(@key, @pass)
  @request.set_form_data(multipart)
  @response = http.request(@request)
  return @response
end

and this is how i call them from the controller

file = {filetype: params[:follow_alert][:sound_url].original_filename,name: params[:follow_alert][:sound_url].original_filename , path: params[:follow_alert][:sound_url].tempfile}
  result = @smartfile.upload_sound(file)

also how i generate the multipart data for upload

  def file_to_multipart(file)
filename = file[:name]
type = file[:filetype]
mime_types = MIME::Types.of(type)
mime_type = mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type
part = %Q|Content-Disposition: form-data; name="#{filename}"; filename="#{filename}"\r\n|
part += "Content-Transfer-Encoding: binary\r\n"
part += "Content-Type: #{mime_type}\r\n\r\n#{file[:path].read}\r\n"
return part

end

can someone with SmartFile(website) upload with rails? i've found a gem on github(smartfile) but its from 2011 and it wont work anymore

thewebsite is smartfile.com/

Aucun commentaire:

Enregistrer un commentaire