我已经尝试使用正文解析器在节点js中解码图像文件并上传到服务器URL.但我没有使用base64图像获得uplaod和parser图像的成功.
if (req.method === 'POST') { //base64_decode(req.body.profileImg, 'copy.jpeg'); //console.log(req.body.profileImg); var NewImageName = Math.random().toString(36).substring(7); var imageBuffer = decodeBase64Image(req.body.profileImg); fs.writeFile('../assets/images/seller/'+NewImageName+'.png', imageBuffer.data, function(err) { });
Punit Gajjar.. 5
您可以尝试使用此代码解码base64图像.
function decodeBase64Image(dataString) { var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/), response = {}; if (matches.length !== 3) { return new Error('Invalid input string'); } response.type = matches[1]; response.data = new Buffer(matches[2], 'base64'); return response; } app.post('/UploadImages', function (req,res){ if (req.method === 'POST') { var NewImageName = Math.random().toString(36).substring(7); var imageBuffer = decodeBase64Image(req.body.profileImg); fs.writeFile('../assets/images/seller/'+NewImageName+'.png', imageBuffer.data, function(err) { }); res.json(200, {profileImgName: NewImageName }); } });
只需在nodeJs函数中获取base64编码的字符串,然后将其发送到我上面创建的解码Image的函数decodeBase64Image,然后就可以上传了.
谢谢
您可以尝试使用此代码解码base64图像.
function decodeBase64Image(dataString) { var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/), response = {}; if (matches.length !== 3) { return new Error('Invalid input string'); } response.type = matches[1]; response.data = new Buffer(matches[2], 'base64'); return response; } app.post('/UploadImages', function (req,res){ if (req.method === 'POST') { var NewImageName = Math.random().toString(36).substring(7); var imageBuffer = decodeBase64Image(req.body.profileImg); fs.writeFile('../assets/images/seller/'+NewImageName+'.png', imageBuffer.data, function(err) { }); res.json(200, {profileImgName: NewImageName }); } });
只需在nodeJs函数中获取base64编码的字符串,然后将其发送到我上面创建的解码Image的函数decodeBase64Image,然后就可以上传了.
谢谢