图片base64转化为二进制文件上传给后端

```

function upload(file){

readBlobAsDataURL(file, function (dataurl){

var formData = new FormData();

formData.append("upload",convertBase64UrlToBlob(dataurl))

$.ajax({

url: 'http://127.0.0.1:3000/upload' ,

type: 'POST',

data: formData,

async: false,

cache: false,

contentType: false,

processData: false,

success: function (returndata) {

},

error: function (returndata) {

}

});

});

}

function readBlobAsDataURL(file, callback) {

var a = new FileReader();

a.onload = function(e) {callback(e.target.result);};

a.readAsDataURL(file);

}

//图片格式转化

function convertBase64UrlToBlob(urlData) {

var bytes = window.atob(urlData.split(',')[1]); //去掉url的头,并转换为byte

//处理异常,将ascii码小于0的转换为大于0

var ab = new ArrayBuffer(bytes.length);

var ia = new Uint8Array(ab);

for (var i = 0; i < bytes.length; i++) {

ia[i] = bytes.charCodeAt(i);

}

return new Blob([ab], {

type: 'image/png'

});

}

```

(参考http://blog.csdn.net/cuixiping/article/details/45932793)

node服务端  https://github.com/xiemingzhang/nodejs_imgUpload

你可能感兴趣的:(图片base64转化为二进制文件上传给后端)