2016-12-22 29 views
0

我越來越瘋狂從數據庫領域獲取物理文件。我使用asp.net MVC項目(dotnet核心1.1)。感謝聖誕快樂![dotnet core MVC],我無法從字節數組(字段存儲到數據庫)中獲取文件。

在我的控制器的某些部分:

public void Download(int id) 
{ 
    downloadMyFile(_context.myTable.Find(id).MyBLOBField, 
       _context.myTable.Find(id).MyFieldContentType, 
       _context.Richieste.Find(id).MyFieldOriginalFileName 
    ); 
} 

public FileContentResult downloadMyFile(byte[] fileBytes, 
            string fileContent, string fileName) 
{ 
      return File(fileBytes, fileContent, fileName); 
} 

我的觀點:

<a asp-action="Download" asp-route-id="@Model.Id"> Download File</a> 

**

我的筆記:

如果我用這個,工作原理:

public FileResult download() 
     { 
      var fileName = @"myRealFileNameStoredInTheServer.ext"; 
      var filepath = $"Downloads/{fileName}"; 
      byte[] fileBytes = System.IO.File.ReadAllBytes(filepath); 
      return File(fileBytes, "application/x-msdownload", fileName); 
     } 

但是當PASS字節數組從另一個字節數組不起作用

我也嘗試使用Response方法,但我無法找到響應的ASPNET CORE重載BinaryWrite()...什麼也不是usin g MemoryStream! 你能幫我嗎..?

+3

你的方法是'void' - 它不返回任何 –

+0

你有一個解決方案? –

+1

使它'public FileContentResult Download(int id)'(並使另一個方法'private')返回downloadMyFile()的結果 - ''return downloadMyFile(.....);' –

回答

0

閱讀Stephen Muecke的意見,瞭解我的 問題。

public FileContentResult Download(int id) 
{ 
    return File(_context.myTable.Find(id).MyBLOBField, 
       _context.myTable.Find(id).MyFieldContentType, 
       _context.Richieste.Find(id).MyFieldOriginalFileName 
    ); 
} 
相關問題