c# - How to convert base64 string to image binary file and save onto server -
this question has answer here:
- c# base64 string jpeg image 4 answers
as example have converted canvas element re-sized image , posted hidden input field that's encoded
data:image/jpeg;base64,/9j/4aaqskzjrgabaqaaaqabaad...
this value posted same page need convert string image , save onto server.
code behind file (upload.aspx)
protected void btnupload_click(object sender, eventargs e) { httppostedfile fileposted = request.files["newinput"]; string base64string = fileposted.tostring(); // convert base64 string byte[] byte[] imagebytes = convert.frombase64string(base64string); memorystream ms = new memorystream(imagebytes, 0, imagebytes.length); // convert byte[] image ms.write(imagebytes, 0, imagebytes.length); system.drawing.image image = system.drawing.image.fromstream(ms, true); //i dont know how write above saveas condition below if (fileposted != null && fileposted.contentlength > 0) { string filenameapplication = path.getfilename(fileposted.filename); string fileextensionapplication = path.getextension(filenameapplication); // generating random guid new file @ server uploaded file string newfile = guid.newguid().tostring() + fileextensionapplication; // getting valid server path save string filepath = path.combine(server.mappath("~/assets/") + request.querystring["id"] + "/", newfile); if (filenameapplication != string.empty) { fileposted.saveas(filepath); } }
i'm pretty sure need convert imagedata binary file before saving on server can't quite how need amend code above. ideas? code save server doesn't work.
once have converted image , changed it's name above - i'm storing database via linq - url appended it.
any appreciated.
hope below functions helps.
public string imagetobase64(image image, system.drawing.imaging.imageformat format) { using (memorystream ms = new memorystream()) { // convert image byte[] image.save(ms, format); byte[] imagebytes = ms.toarray(); // convert byte[] base64 string string base64string = convert.tobase64string(imagebytes); return base64string; } } public image base64toimage(string base64string) { // convert base64 string byte[] byte[] imagebytes = convert.frombase64string(base64string); memorystream ms = new memorystream(imagebytes, 0, imagebytes.length); // convert byte[] image ms.write(imagebytes, 0, imagebytes.length); image image = image.fromstream(ms, true); return image; }
edit 1 -
from comments seems getting base64 string , need save image on server , whenever required need show image using physical server path.
ok. base64toimage give image base64 string. can save on server using
image.save("path", system.drawing.imaging.imageformat.jpeg);
and "path" have supplied or created can stored in db url, can use @ time of display.
note: make sure have write access folder saving image.
edit-2 function should below. please put validation code, error handling required.
protected void btnupload_click(object sender, eventargs e) { httppostedfile fileposted = request.files["newinput"]; string base64string = fileposted.tostring(); // convert base64 string byte[] byte[] imagebytes = convert.frombase64string(base64string); memorystream ms = new memorystream(imagebytes, 0, imagebytes.length); // convert byte[] image ms.write(imagebytes, 0, imagebytes.length); system.drawing.image image = system.drawing.image.fromstream(ms, true); string newfile = guid.newguid().tostring() + fileextensionapplication; string filepath = path.combine(server.mappath("~/assets/") + request.querystring["id"] + "/", newfile); image.save(filepath,imageformat.jpeg); }
Comments
Post a Comment