2012-10-13 20 views
0

的WebAPI控制器我使用的Web API控制器如下:的HttpResponse在MVC

[HttpPost]  
public HttpResponseMessage PostMethod(string filename)  
{ 
    Stream downloadStream = BL.method(fileName);  
    HttpResponseMessage response = new HttpResponseMessage();  
    response.content= new StreamContent(downloadStream);  
    return response;  
} 

當我嘗試使用招我正在一個異常說

「downloadStream調用上述方法。 ReadTimeout'拋出了一個類型爲 'System.InvalidOperationException'的異常。

可以設置流的響應和發送?上述代碼是否有任何修改?

回答

0

您的信息流似乎存在問題。不知道如何生成流,很難說。如果你使用FileStream自己加載文件來代替BL.method(fileName);,這應該可行(我只是自己測試過)。

在邊注,有幾個問題,你的方法:

  1. 您使用POST。既然你沒有改變什麼,GET就更好了。
  2. 您沒有設置ContentType頭所以客戶端可以使用資源
  3. 你沒有配置流所以這個流可以留在地獄一般不會有好問題。
+0

其實我不能代替BL.method(fileName);與文件流,因爲我從數據庫流這個文件儀式。我通過給它的文件名從數據庫中獲取流。是否有任何解決方案呢? – user1599992

+0

似乎有沒有在Web API中的錯誤。 – Aliostad

+0

確定我可以顯式設置流的readtimeout屬性嗎? – user1599992

0

嘗試使用PushStreamContent,也許不通過緩衝內存中的文件,可能會避免超時。

[HttpPost] 
    public HttpResponseMessage PostMethod(string filename) 
    { 
     Stream downloadStream = BL.method(fileName); 
     HttpResponseMessage response = new HttpResponseMessage(); 
     response.Content = new PushStreamContent((responseStream, httpContent, tc) => { 
                downloadStream.CopyTo(responseStream); 
                responseStream.Close(); 
               }, "application/octet-stream"); 

     return response; 
    }