c# - Writing images to folder (ASP.NET MVC) -
i have webapi project , need image mobile app , write folder.
here code
public class imagecontroller : apicontroller { public bool saveimage(string imgstr, string imgname) { string path = httpcontext.current.server.mappath("~/image"); //path //check if directory exist if (!system.io.directory.exists(path)) { system.io.directory.createdirectory(path); //create directory if doesn't exist } string imagename = imgname + ".jpg"; //set image path string imgpath = path.combine(path, imagename); byte[] imagebytes = convert.frombase64string(imgstr); file.writeallbytes(imgpath, imagebytes); return true; } }
and here url see in api tab
http://localhost:55341/api/image?imgstr={imgstr}&imgname={imgname}
i send 2 parameters in request. imgstr base 64 sting , imgname static name - test.
but have
{"message":"an error has occurred.","exceptionmessage":"the input not valid base-64 string contains non-base 64 character, more 2 padding characters, or illegal character among padding characters. ","exceptiontype":"system.formatexception","stacktrace":" @ system.convert.frombase64_decode(char* startinputptr, int32 inputlength, byte* startdestptr, int32 destlength)\r\n @ system.convert.frombase64charptr(char* inputptr, int32 inputlength)\r\n @ system.convert.frombase64string(string s)\r\n @ trackingappbackend.controllers.imagecontroller.saveimage(string imgstr, string imgname) in c:\users\nemes\documents\visual studio 2017\projects\trackingappbackend\trackingappbackend\controllers\imagecontroller.cs:line 29\r\n @ lambda_method(closure , object , object[] )\r\n @ system.web.http.controllers.reflectedhttpactiondescriptor.actionexecutor.<>c__displayclass10.b__9(object instance, object[] methodparameters)\r\n @ system.web.http.controllers.reflectedhttpactiondescriptor.actionexecutor.execute(object instance, object[] arguments)\r\n @ system.web.http.controllers.reflectedhttpactiondescriptor.executeasync(httpcontrollercontext controllercontext, idictionary`2 arguments, cancellationtoken cancellationtoken)\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.controllers.apicontrolleractioninvoker.d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.controllers.actionfilterresult.d__2.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.dispatcher.httpcontrollerdispatcher.d__1.movenext()"}
but send valid base64 string.
where can problem?
base64 string can contain "+", "=" , "/" isn't valid characters in querystring. think shouldn't pass image base64 in querystring. try use body of post/put.
in case, exception says:
more 2 padding characters, or illegal character among padding characters.
Comments
Post a Comment