I'm using the following code, to download an image from specific URL and upload it to a media folder:
public class ImageService
{
private readonly IMediaService _mediaService;
public ImageService(IMediaService mediaService)
{
_mediaService = mediaService;
}
public int UploadMedia(string imageSrc)
{
IMedia newImage = null;
try
{
var mediaSvc = _mediaService;
var mediaExtension = Path.GetExtension(imageSrc);
var imageName = Guid.NewGuid() + mediaExtension;
var image = DownloadImageFromUrl(imageSrc);
const string rootPath = "~\\media";
var fileName =
HttpContext.Current.Server.MapPath(Path.Combine(rootPath, imageName));
image.Save(fileName);
var parentFolderId = 5177;
newImage = mediaSvc.CreateMedia(imageName, parentFolderId, "Image");
var buffer = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));
newImage.SetValue("umbracoFile", new MemoryStream(buffer));
mediaSvc.Save(newImage);
if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~\\media\\" + imageName)))
{
System.IO.File.Delete(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));
}
}
catch (Exception ex)
{
// log the exception
}
return newImage.Id;
}
private System.Drawing.Image DownloadImageFromUrl(string imageUrl)
{
System.Drawing.Image image = null;
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
webRequest.AllowWriteStreamBuffering = true;
webRequest.Timeout = 30000;
var webResponse = webRequest.GetResponse();
var stream = webResponse.GetResponseStream();
image = System.Drawing.Image.FromStream(stream);
webResponse.Close();
}
catch (Exception ex)
{
// log the exception
}
return image;
}
}
downloading and saving in to the file system works. the image file can be opened and all good. It gets also uploaded to the subfolder with the ID in the media folder, but the uploaded image file is not really an image file, looks like a zombie.
It doesn't have a thumbnail, no width, height, size, ... I also can't open the image. see the screenshot
it seems to me like you are not using the umbraco.core extension method setValue. If you click on the method name SetValue and press F12, you should be navigated to Umbraco.Core.ContentExtensions.SetValue method, if not, then something is wrong with your call.
I wonder why you are not using Current.Services but directly the Services class.
This is how it works for me:
private static IMedia AddOrEditReferenceImage(MemberModel model, IMember member, int mediaId)
{
var rootMedia = Current.Services.MediaService.GetRootMedia().First();
long totalrecords = 0;
var children = Current.Services.MediaService.GetPagedChildren(rootMedia.Id, 0, 1000, out totalrecords);
var folder = children.Where(x => x.Name.Equals(member.Name)).FirstOrDefault();
if (folder == null)
{
folder = Current.Services.MediaService.CreateMedia(member.Name, rootMedia.Id, "Folder");
Current.Services.MediaService.Save(folder);
}
var newImage = Current.Services.MediaService.CreateMedia(model.CompanyCaseStudies.AddedImage.FileName, folder.Id, "Image");
newImage.SetValue(Current.Services.ContentTypeBaseServices, "umbracoFile", model.CompanyCaseStudies.AddedImage.FileName, model.CompanyCaseStudies.AddedImage.InputStream);
Current.Services.MediaService.Save(newImage);
if (mediaId != -1)
Current.Services.MediaService.Delete(Current.Services.MediaService.GetById(mediaId));
return newImage;
}
Upload Image to media folder
Hi,
I'm using the following code, to download an image from specific URL and upload it to a media folder:
downloading and saving in to the file system works. the image file can be opened and all good. It gets also uploaded to the subfolder with the ID in the media folder, but the uploaded image file is not really an image file, looks like a zombie.
It doesn't have a thumbnail, no width, height, size, ... I also can't open the image. see the screenshot
any ideas?
thanks
dee
needed to use the extension SetValue-method of Umbraco.Core instead:
I am getting an error when trying exactly this solution.
What do I have to do to make sure I can use this extension method please?
Send your code pls
I posted the code and link to this thread on stack overflow as a question:
https://stackoverflow.com/questions/62310126/umbraco-8-setvalue-has-some-invalid-arguments-saving-new-image-to-media-c-sha
Thanks
it seems to me like you are not using the umbraco.core extension method setValue. If you click on the method name SetValue and press F12, you should be navigated to Umbraco.Core.ContentExtensions.SetValue method, if not, then something is wrong with your call.
I wonder why you are not using Current.Services but directly the Services class.
This is how it works for me:
by using Umbraco.Core import. Hope this helps you
I have fixed this by changing the lines
dynamic newImage = null; newImage = mediaSvc.CreateMedia(imageName, -1, "Image");
to:
var newImage = mediaSvc.CreateMedia(imageName, -1, "Image");
So it must be the
dynamic
keyword that was causing the method to match to the wrong signature, not the extension method.great!
is working on a reply...