问)我怎样才能修复下面的方法调用,使它有有效的参数?
问题背景:
基于这里的这篇文章... - 我正在尝试从 URL 下载图像(为简洁起见,下面硬编码),然后将其保存到媒体文件夹。
我收到一个错误:
The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetValue(string, object, string, string)' has some invalid arguments
进口:
using Umbraco.Web.WebApi;
using System.Collections.Generic;
using System.Net;
using System;
using System.Web;
using System.IO;
using Umbraco.Core;
方法细节:
var imageSrc = "https://images.isbndb.com/covers/81/38/9781538748138.jpg";
try
{
var mediaSvc = Services.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);
dynamic newImage = null;
// -1 is the root folderID, i.e. Media. All images will be saved under Media in Umbraco
newImage = mediaSvc.CreateMedia(imageName, -1, "Image");
var buffer = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("~\\media\\" + imageName));
newImage.SetValue(Services.ContentTypeBaseServices, "umbracoFile", imageName, 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));
}
return newImage.Id;
}
catch(Exception ex) {
return ex;
}