2013-09-05 52 views
2

我想從文件系統中讀取一個圖像文件,並將它保存爲Umbraco中的MediaItemUmbraco MediaService/Umbraco MediaItem not saving

這是我放在一起的代碼:

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的跡象。

我也驗證過saveAs方法的memoryfile(這是一個HttpPostedFileBase)永遠不會被調用。

任何人都可以幫助我,並指出我正確的方向來解決這個問題嗎?

+0

你需要進一步的幫助嗎? – dmportella

回答

3

保存媒體,我發現這個方法與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}); 
    } 
+0

如果你沒有父文件夾......並且沒有父母ID,我試過「-1」或「null」無濟於事? – Jimmyt1988

3

媒體對象有不同的反應對某些對象類型傳遞給它,在文件的情況下存在的子類來處理的HTTPPostedFile這是一個覆蓋僅在文件發佈事件中創建,但基類HttpPostedFileBase(這是Umbraco引用的內容)可以通過繼承和實現,因此您可以將文件傳遞到媒體服務並使Umbraco創建正確的文件路徑等。

在示例中下面我創建了一個名爲FileImportWrapper的新類,該類繼承自HttpPostedFilebase,然後繼續覆蓋所有屬性和工具nt我的版本。

對於內容類型,我使用了此計算器中提供的代碼示例(File extensions and MIME Types in .NET)。

請參閱下面的示例代碼。

類代碼

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); 

我希望這有助於你和其他有同樣的要求。