jquery批量上传图片 java_前台页面上传data image图片,java后台接收图片保存

最近在项目中有这么一个需求,就是上传一个视频文件,然后要获取视频文件的第一帧图片,这个可以通过canvas获取得到,得到的是一个dataurl,之后还要将这个图片上传到云,这个时候如何操作就不清楚了,于是乎,google一番,总结如下:

将dataurl转成blob

利用formdata

异步上传

function b64toblob(b64data, contenttype='', slicesize=512) {

const bytecharacters = atob(b64data);

const bytearrays = [];

for (let offset = 0; offset < bytecharacters.length; offset += slicesize) {

const slice = bytecharacters.slice(offset, offset + slicesize);

const bytenumbers = new array(slice.length);

for (let i = 0; i < slice.length; i++) {

bytenumbers[i] = slice.charcodeat(i);

}

const bytearray = new uint8array(bytenumbers);

bytearrays.push(bytearray);

}

const blob = new blob(bytearrays, {type: contenttype});

return blob;

}

// dataurl to blob

// 假设一个dataurl

const imageurl = "转成base64的变量";

// split the base64 string in data and contenttype

const block = imageurl.split(";");

// get the content type of the image

const contenttype = block[0].split(":")[1];// in this case "image/jpeg"

// get the real base64 content of the file

const realdata = block[1].split(",")[1];// in this case "r0lgodlhpqbeapeoajosm...."

// convert it to a blob to upload

var blob = b64toblob(realdata, contenttype);

// new a formdata

const formdata = new formdata();

formdata.append('blob', blob);

// upload

$.ajax({

url:url,

data: formdata

type:"post",

contenttype:false,

processdata:false,

error:function(err){

},

success:function(data){

},

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值