最近移動端界面給後臺傳數據時用到Blob,它能夠看作是存放二進制數據的容器;json
//上傳圖片數據封裝 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字符串。數組