![](/img/trans.png)
[英]Umbraco Mediaservice outofmemory exception but file is uploaded
[英]Umbraco MediaService / Umbraco MediaItem not saving
我正在嘗試從文件系統讀取圖像文件,並將其另存為Umbraco中的MediaItem
。
這是我放在一起的代碼:
MemoryStream uploadFile = new MemoryStream();
using (FileStream fs = File.OpenRead(tempFilename))
{
fs.CopyTo(uploadFile);
HttpPostedFileBase memoryfile = new MemoryFile(uploadFile, mimetype, Path.GetFileName(src.Value));
IMedia mediaItem = _mediaService.CreateMedia(Path.GetFileName(src.Value), itNewsMediaParent, "Image");
mediaItem.SetValue("umbracoFile", memoryfile);
_mediaService.Save(mediaItem);
src.Value = library.NiceUrl(mediaItem.Id);
}
不幸的是,似乎Umbraco無法將文件保存在媒體文件夾中。 它確實在“媒體”樹中創建了節點,並設置了正確的寬度,高度以及所有其他內容。 但是,它指向/media/1001/my_file_name.jpg
。 媒體部分已經包含多個圖像,下一個應使用的“ ID”為“ 1018”。 另外,如果我在/media/1001
內部檢查,則沒有my_file_name.jpg
。
我還驗證了,永遠不會調用memoryfile的SaveAs方法(這是一個HttpPostedFileBase)。
誰能協助我,並指出正確的方向來解決這個問題?
為了保存媒體,我在MediaService中找到了此方法。 但是,我認為可能還有另一種更完善的方法
[HttpPost]
public JsonResult Upload(HttpPostedFileBase file)
{
IMedia mimage;
// Create the media item
mimage = _mediaService.CreateMedia(file.FileName, <parentId>, Constants.Conventions.MediaTypes.Image);
mimage.SetValue(Constants.Conventions.Media.File, file);
_mediaService.Save(mimage);
return Json(new { success = true});
}
Media對象對傳遞給它的某些對象類型的反應不同,在文件的情況下,子類中有一個重寫處理HTTPPostedFile,該重寫僅在文件發布事件期間創建,但是基類HttpPostedFileBase(這是Umbraco引用的) )可以繼承並實現,因此您可以將文件傳遞到媒體服務,並讓Umbraco創建正確的文件路徑等。
在下面的示例中,我創建了一個名為FileImportWrapper的新類,該類從HttpPostedFilebase繼承,然后繼續覆蓋所有屬性並實現我的版本。
對於內容類型,我使用了此stackoverflow帖子中介紹的代碼示例( .NET中的文件擴展名和MIME類型 )。
請參見下面的示例代碼。
班級代碼
public sealed class FileImportWrapper : HttpPostedFileBase
{
private FileInfo fileInfo;
public FileImportWrapper(string filePath)
{
this.fileInfo = new FileInfo(filePath);
}
public override int ContentLength
{
get
{
return (int)this.fileInfo.Length;
}
}
public override string ContentType
{
get
{
return MimeExtensionHelper.GetMimeType(this.fileInfo.Name);
}
}
public override string FileName
{
get
{
return this.fileInfo.FullName;
}
}
public override System.IO.Stream InputStream
{
get
{
return this.fileInfo.OpenRead();
}
}
public static class MimeExtensionHelper
{
static object locker = new object();
static object mimeMapping;
static MethodInfo getMimeMappingMethodInfo;
static MimeExtensionHelper()
{
Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping");
if (mimeMappingType == null)
throw new SystemException("Couldnt find MimeMapping type");
ConstructorInfo constructorInfo = mimeMappingType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
if (constructorInfo == null)
throw new SystemException("Couldnt find default constructor for MimeMapping");
mimeMapping = constructorInfo.Invoke(null);
if (mimeMapping == null)
throw new SystemException("Couldnt find MimeMapping");
getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic);
if (getMimeMappingMethodInfo == null)
throw new SystemException("Couldnt find GetMimeMapping method");
if (getMimeMappingMethodInfo.ReturnType != typeof(string))
throw new SystemException("GetMimeMapping method has invalid return type");
if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(string))
throw new SystemException("GetMimeMapping method has invalid parameters");
}
public static string GetMimeType(string filename)
{
lock (locker)
return (string)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename });
}
}
}
使用代碼
IMedia media = ApplicationContext.Current.Services.MediaService.CreateMedia("image test 1", -1, Constants.Conventions.MediaTypes.Image);
this.mediaService.Save(media); // called it before so it creates a media id
FileImportWrapper file = new FileImportWrapper(IOHelper.MapPath("~/App_Data/image.png"));
media.SetValue(Constants.Conventions.Media.File, file);
ApplicationContext.Current.Services.MediaService.Save(media);
希望對您和其他有相同要求的人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.