2014-04-03 38 views
1

我現在有一個包含鏈接的HTML頁面的用戶:ASP.NET MVC:檢索.mp3文件,並恢復

<a href="javascript:void()" onclick="return getAudioFile('fileLocation');">Listen</a> 

點擊「試聽」鏈接調用這個函數:

function getRecordingFile(fileLocation) { 

    }; 

最終應調用該控制器的方法:

[HttpPost] 
    public ActionResult GetAudioFile(string fileLocation) 
    { 
     return null; 
    } 

我已經掏空了的功能和方法b因爲我已經嘗試了幾個不同的事情來完成這個任務:我需要從本地位置訪問音頻文件,並允許用戶聽取它/點擊Listen鏈接下載它。

什麼似乎是最好的方式去做這件事?

+0

您可以渲染一個

+0

在網頁上渲染音頻控件會很好,但經過一番挖掘,看起來我必須對IIS進行一些更改。我想避免這種情況。如果「下載」文件是唯一的選擇,那麼我會好的。 –

回答

1

下面是最終的結果:

[HttpGet] 
    public ActionResult GetAudioFile(string fileLocation) 
    { 
     var bytes = new byte[0]; 


     using (var fs = new FileStream(fileLocation, FileMode.Open, FileAccess.Read) 
     { 
      var br = new BinaryReader(fs); 
      long numBytes = new FileInfo(fileLocation).Length; 
      buff = br.ReadBytes((int)numBytes); 
     } 

     return File(buff, "audio/mpeg", "callrecording.mp3"); 
    } 

在頁面的鏈接是:

<a href="/Controller/[email protected]">Listen</a> 

榮譽給我的老闆的幫助。

0

也許使用FileResult?

[HttpPost] 
public FileResult GetAudioFile(string fileLocation) 
{ 
    using(FileStream fs = new FileStream(fileLocation)){ 

     return File(fs.ToArray(), "audio/mpeg"); 
    } 

} 
+0

我做了類似的事情。所有代碼都會執行,但網頁上沒有任何反應。我希望提示下載文件。 –