c# - Sitecore: Saving images in media library from url -
to download , save image in media library using following code. can see item created in media library has no media.
using (webclient webclient = new webclient()) { byte[] data = webclient.downloaddata("https://myurl/images?id="+12345); stream memorystream = new memorystream(data); var options = new sitecore.resources.media.mediacreatoroptions { filebased = false, overwriteexisting = true, versioned = true, includeextensioninitemname = true, destination = factory.getdatabase("master").getitem(settings.getsetting("profilepicturesfolderitemid")).paths.path + "/" + "12345", database = factory.getdatabase("master"), alternatetext = userprofileitem.name }; using (new securitydisabler()) { var creator = new sitecore.resources.media.mediacreator(); creator.createfromstream(memorystream, v1imageid, options); } }
in media folder can see item name, "12345" there no media.
from remember, when pass destination
in options, has contain new name of item , should same second argument in createfromstream
method call without extension:
using (webclient webclient = new webclient()) { string filename = "somefilename"; string extension = ".png"; // or whatever extension byte[] data = webclient.downloaddata(imageurl); stream memorystream = new memorystream(data); var options = new sitecore.resources.media.mediacreatoroptions { filebased = false, overwriteexisting = true, versioned = true, includeextensioninitemname = true, destination = factory.getdatabase("master").getitem(settings.getsetting("profilepicturesfolderitemid")).paths.path + "/" + filename, database = factory.getdatabase("master"), alternatetext = userprofileitem.name }; using (new securitydisabler()) { var creator = new sitecore.resources.media.mediacreator(); creator.createfromstream(memorystream, filename + extension, options); } }
Comments
Post a Comment