Umbraco 8:SetValue 有一些无效参数 - 将新图像保存到媒体 C#

Umbraco 8: SetValue has some invalid arguments - Saving new image to media C#

Q) 我怎样才能修复下面的方法调用以使其具有有效参数?

问题背景:

基于 this post here... - 我正在尝试从 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;
}

您根本不应该自己在 ~/media 中进行保存。该路径由 Umbraco 设置,可以被自定义提供者覆盖。所以将那部分留给 MediaService。

此外,当您保存一个媒体项目时,您不应该(您不能)给它一个路径,只给一个文件名,我预计这也会给您带来问题。

你看过这里的文档了吗? https://our.umbraco.com/documentation/Reference/Management/Services/MediaService/

我已经通过更改行解决了这个问题

dynamic newImage = null;
newImage = mediaSvc.CreateMedia(imageName, -1, "Image");

至:

var newImage = mediaSvc.CreateMedia(imageName, -1, "Image");

所以一定是 dynamic 关键字导致方法匹配到错误的签名,而不是扩展方法。