最近移动端界面给后台传数据时用到Blob,它可以看做是存放二进制数据的容器;
//上传图片数据封装 function uploadPhotoData(data,fileName){ var imgArray = new Array(); if(data == null || data == ""){ return null; } var img = { fileName: encodeURI(fileName, "UTF-8"), fileStr: data }; imgArray[0] = img; var blob = new Blob([JSON.stringify(imgArray).toString()], { type: 'text/plain' }); return blob; }
其中encodeURI(fileName, "UTF-8")是将fileName转码为UTF-8,防止乱码;JSON.stringify(imgArray).toString()是将imgArray数组转化为json字符串。