c# - ASP.NET Save Image in ashx handler -
i have simple handler (ashx) receives base 64 encoded image , want save image.
here code:
sub processrequest(byval context httpcontext) implements ihttphandler.processrequest dim imageinfo = context.request("imgdata") dim imagedata string() = imageinfo.split(",") ' check there 2 parts; ie header , string data. if imagedata.length < 2 context.response.write("error") return end if ' example of imageinfo ' data:image/png;base64,ivborw0kggoaaaansuheugaaae... dim imagetype string = imagedata(0) dim imagestring string = imagedata(1) if imagetype <> "data:image/png;base64" context.response.write("error") return end if imagestring = imagestring.replace(" ", "+") dim bytes byte() bytes = convert.frombase64string(imagestring) dim image system.drawing.image using ms memorystream = new memorystream(bytes) image = system.drawing.image.fromstream(ms) end using image.save("test", system.drawing.imaging.imageformat.png) end sub
however, following error:
an exception of type 'system.runtime.interopservices.externalexception' occurred in system.drawing.dll not handled in user code
additional information: generic error occurred in gdi+
i know image data works in php version. trying convert php asp.net.
/*get file name , image data post*/ $filename = $_post['filename']; $data = $_post['imgdata']; list( $type, $data ) = explode( ';', $data ); list( $base, $data ) = explode( ',', $data ); /*check image data base64 of png*/ if ( $type === 'data:image/png' && $base === 'base64') { $data = base64_decode( $data ); file_put_contents( '../pathtosave/' . $filename, $data ); echo 'saved'; } else { echo 'error'; }
inspecting image in debugger, appears okay. correctly gets height , width of image.
can see error may be?
i embarrassed. issue "test" not correct file path. testing on local machine , used full path, worked straight away.
Comments
Post a Comment