2009-12-23 105 views
3

任何人都可以給我一個快速指示器,告訴我如何獲得一個返回字節數組的OpenRasta處理程序。在ResourceSpace中公開而不是JSON或XML對象。即我不希望它被轉碼,我只是希望能夠將媒體類型設置爲「圖像/ PNG」或類似。使用OpenRasta作爲流或字節數組獲取圖像

使用ASP.Net MVC我可以通過返回

File(myByteArray, "image/PNG"); 

我只需要知道OpenRasta相當於使用FileContentResult它做。

感謝

回答

10

你可以只返回一個字節數組作爲你的handlerm的一部分,但最終會作爲application/octet-stream服務。

如果你想返回文件,你可以簡單地返回一個IFile的實現。

public class MyFileHandler { 
    public IFile Get(int id) { 
    var mybytes = new byte[]; 
    return new InMemoryFile(new MemoryStream(mybytes)) { 
     ContentType = new MediaType("image/png"); 
    } 
    } 
} 

您還可以設置文件名屬性返回一個特定的文件名,這會使一個Content-Disposition首部爲您服務。

0

那麼有一些流編解碼器在那裏,但你可以簡單地做到這一點,因爲這

ResourceSpace.Has.ResourcesOfType<byte[]>() 
       .AtUri("/MyImageUri") 
       .HandledBy<ImageHandler>(); 

其中圖像處理程序返回從System.Drawing.Graphics對象所做的字節數組在我的情況。

任何其他解釋這個話題更多的燈光將不勝感激。

2

我看這件事的OpenRasta郵件列表上,並有一對夫婦相關的帖子: http://groups.google.com/group/openrasta/browse_thread/thread/5ae2a6d653a7421e# http://groups.google.com/group/openrasta/browse_thread/thread/a631d3629b25b88a#

我已經得到了它具有下列樣品準備:

配置:

ResourceSpace.Has.ResourcesOfType<IFile>() 
    .AtUri("/customer/{id}/avatar") 
    .HandledBy<CustomerAvatarHandler>(); 

處理程序:

public class CustomerAvatarHandler 
{ 
    public object Get(int id) 
    { 
     const string filename = @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg"; 
     return new InMemoryFile(File.OpenRead(filename)); 
    } 
} 
+0

這並不回答你的具體問題re byte [],但我相信你可以通過MemoryStream或其他東西把一個字節數組轉換成Stream。 – 2009-12-23 12:04:09

相關問題