c# - Read Image as base64 string; Out of memory exception -
i'm trying convert images in directory base64 strings. when test in local computer works, on server gives out of memory exception: here method:
private imagedataviewmodel readimagesfromdisk(guid id) { list<string> imagelist = new list<string>(); imagedataviewmodel imagedataviewmodel = new imagedataviewmodel(); string path = httpcontext.current.server.mappath("~/businessimages/" + id.tostring()); try { if (directory.exists(path)) { var images = directory.getfiles(path, "*.*", searchoption.alldirectories) .tolist(); string filename = id.tostring(); foreach (var item in images) { using (image image = image.fromfile(item)) { using (memorystream m = new memorystream()) { image.save(m, image.rawformat); byte[] imagebytes = m.toarray(); // convert byte[] base64 string string base64string = convert.tobase64string(imagebytes); imagelist.add(base64string); } } } imagedataviewmodel.id = id; imagedataviewmodel.imagedata = imagelist; return imagedataviewmodel; } else { return null; } } catch (exception ex) { throw ex; } }
i don't know how happens. there way handle problem?
byte[] filebytes = system.io.file.readallbytes(yourfilepath + fname); string filename = yourfilename.split(new string[] { "/" }, stringsplitoptions.none)[1]; return file(filebytes, system.net.mime.mediatypenames.application.octet, filename);
try this, return whole file.
Comments
Post a Comment