Saving Base64 Image with ADODB.Stream and Classic ASP

Asked

Viewed 310 times

0

I’m using Classic ASP and Javascript to upload and crop the images to the site. I use a script that converts the images to Base64 sends by post and is processed on the server.

I followed step by step through here: Saving Base64 Image with ADODB.Stream

Prior: http://codepen.io/bigaton/pen/NRBKaa

It turns out that when resized my image to a certain size error occurs while saving:

msxml3.dll error '80004005' error analyzing 'bla bla img Base64' as bin.Base64 data type.

save2.Asp

base64String = Trim(Request.Form("cropped2"))

response.write base64String

Set tmpDoc = Server.CreateObject("Msxml2.DOMDocument")
Set nodeB64 = tmpDoc.CreateElement("b64")
nodeB64.DataType = "bin.base64"
nodeB64.Text = Mid(base64String, InStr(base64String, ",") + 1)

set bStream = server.CreateObject("ADODB.stream")

bStream.type = 1

call bStream.Open()

call bStream.Write(nodeB64.NodeTypedValue)

caminho=Server.MapPath("/teste/imagem.png")
call bStream.SaveToFile(caminho, 2)

call bStream.close()
set bStream = nothing

Images up to 550x400 save normally, 550x450 error occurs and above these values also.

1 answer

0

Problem solved. Instead of sending the Base64 string by HTML POST ( where I loaded the string in a hidden field), send by AJAX POST.

   document.querySelector('#btnCrop').addEventListener('click', function(){
   var img = cropper.getDataURL()
   $.post('ajax/salvar2.asp', {imagem: img});

This post can be used in PHP and ASP.

Example save Base64 to ASP image. (save 2.Asp)

base64String = Trim(Request.Form("imagem"))
Set tmpDoc = Server.CreateObject("MSXML2.DomDocument")
Set nodeB64 = tmpDoc.CreateElement("b64")
nodeB64.DataType = "bin.base64"
nodeB64.Text = Mid(base64String, InStr(base64String, ",") + 1)
set bStream = server.CreateObject("ADODB.stream")
bStream.type = 1
call bStream.Open()
call bStream.Write(nodeB64.NodeTypedValue)
caminho=Server.MapPath("../caminho_salvaer/imagem.png")
call bStream.SaveToFile(caminho, 2)
call bStream.close()
set bStream = nothing

save php.

<?php

// Recuperando imagem em base64
// Exemplo: data:image/png;base64,AAAFBfj42Pj4
$imagem = $_POST['imagem'];

// Separando tipo dos dados da imagem
// $tipo: data:image/png
// $dados: base64,AAAFBfj42Pj4
list($tipo, $dados) = explode(';', $imagem);

// Isolando apenas o tipo da imagem
// $tipo: image/png
list(, $tipo) = explode(':', $tipo);

// Isolando apenas os dados da imagem
// $dados: AAAFBfj42Pj4
list(, $dados) = explode(',', $dados);

// Convertendo base64 para imagem
$dados = base64_decode($dados);

// Gerando nome aleatório para a imagem
$nome = md5(uniqid(time()));

// Salvando imagem em disco
file_put_contents("../img/{$nome}.jpg", $dados);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.