Stream MP3 file MVC3

前端 未结 1 948
梦谈多话
梦谈多话 2021-02-10 02:33

I am trying to stream a file using the audio HTML5 tag. I have put the Controller action to return a FileStream and attached it to the src for the audio. However, the content

1条回答
  •  北恋
    北恋 (楼主)
    2021-02-10 03:05

    You should not return a FileStream, you should return a FileStreamResult or a FilePathResult from your controller action, like this:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult MyAudio()
        {
            var file = Server.MapPath("~/app_data/test.mp3");
            return File(file, "audio/mp3");
        }
    }
    

    and the ~/Views/Home/Index.cshtml view:

    @{
        Layout = null;
    }
    
    
    
        
        Sound Sample
    
    
        

    Some audio

    0 讨论(0)
提交回复
热议问题