我正在使用以下代码从Site System.IO.Stream
上传Sitecore媒体库中的图片。
var creator = new Sitecore.Resources.Media.MediaCreator();
creator.CreateFromStream(stream, e.UploadedFile.SourceName, options);
很少有细节:
e.UploadedFile.SourceName:
是上传文件的路径
var options = new Sitecore.Resources.Media.MediaCreatorOptions
{
FileBased = false,
KeepExisting = true,
Versioned = true,
Destination = // A 'Media Folder' item in media library ,
Database = Factory.GetDatabase("master"),
AlternateText = "some text"
};
只要我上传图片,上面的代码就可以了。我试图上传一张破损的图片,然后我得到了例外:
Exception Details: System.ArgumentException: Parameter is not valid.
此行 :creator.CreateFromStream(stream, e.UploadedFile.SourceName, options);
我尝试使用Try,Catch处理它如下,但它不起作用。
try
{
var creator = new Sitecore.Resources.Media.MediaCreator();
MediaItem mediaItem = creator.CreateFromStream(stream, e.UploadedFile.SourceName, options);
}
catch (Exception ex)
{
throw new ApplicationException("Something wrong happened in the calculation module :", ex);
}