javascript - Uploading Base64 image data to S3 via AWS Ruby SDK -
javascript - Uploading Base64 image data to S3 via AWS Ruby SDK -
i've got drag , drop function takes file that's been dropped on , converts base64 data. before, uploading imgur, api supports base64 uploads, , i'm working on moving amazon s3.
i've seen examples of people using xmlhttp requests , cors upload info s3, i'm using amazon's aws s3 sdk gem avoid having sign policies , other things, gem me. i've done send base64 info local controller metod uses gem upload s3.
the other posts using ajax i've seen show s3 supports raw info uploads, gem doesn't seem to, whenever view uploads broken images. uploading incorrectly? info in wrong format? i've tried basic base64, atob base64, , blob urls, nil works far.
js:
fr.onload = function(event) { var tresult = event.target.result; var datatype = tresult.slice(tresult.search(/\:/)+1,tresult.search(/\;/)); var blob = atob(tresult.replace(/^data\:image\/\w+\;base64\,/, '')); $.ajax({ type:"post", data:{ file:blob, contenttype: datatype, extension:datatype.slice(datatype.search(/\//)+1) }, url:'../uploads/images', success:function(msg) { handlestatus(msg,"success"); }, error:function(errormsg) { handlestatus(errormsg,"error"); } }); }
controller method:
def supload s3 = aws::s3.new(:access_key_id => env['s3_key'],:secret_access_key => env['s3_secret']) bucket = s3.buckets['bucket-name'] info = params[:file].to_s type = params[:contenttype].to_s extension = params[:extension].to_s name = ('a'..'z').to_a.shuffle[0..7].join + ".#{extension}" obj = bucket.objects.create(name,data,{content_type:type,acl:"public_read"}) url = obj.public_url().to_s render text: url end
edit:
to clear, i've tried couple of different formats, 1 displayed above decoded base64. regular base64 looks this:
var tresult = event.target.result; var datatype = tresult.slice(tresult.search(/\:/)+1,tresult.search(/\;/)); var blob = tresult; $.ajax({ type:"post", data:{ file:blob, mimetype: datatype, extension:datatype.slice(datatype.search(/\//)+1) }, url:'../uploads/images', success:function(msg) { handlestatus(msg,"success"); }, error:function(errormsg) { handlestatus(errormsg,"error"); } });
and blob url looks this:
var blob = url.createobjecturl(datauritoblob(tresut,datatype)); ... function datauritoblob(datauri, datatype) { var binary = atob(datauri.split(',')[1]); var array = []; for(var = 0; < binary.length; i++) { array.push(binary.charcodeat(i)); } homecoming new blob([new uint8array(array)], {type: datatype}); }
am reading right are:
using ajax send base64-encoded file rails using rails upload file s3 viewing file in s3?if that's case, need decode info in step 2 before sending on s3. might work:
require "base64" def supload s3 = aws::s3.new(:access_key_id => env['s3_key'],:secret_access_key => env['s3_secret']) bucket = s3.buckets['bucket-name'] info = base64.decode64(params[:file].to_s) type = params[:contenttype].to_s extension = params[:extension].to_s name = ('a'..'z').to_a.shuffle[0..7].join + ".#{extension}" obj = bucket.objects.create(name,data,{content_type:type,acl:"public_read"}) url = obj.public_url().to_s render text: url end
javascript ruby-on-rails ruby amazon-web-services amazon-s3
Comments
Post a Comment