Streaming/ Playing an audio file from FTP

In General FTP doesn't allow to stream a file directly from FTP site. In this post, I will explain how, I did it in an ASP.NET project to play the audio file from FTP. My aim is to play an audio file which is in FTP using HTML audio tag without downloading the file to local folder (server folder). So as FTP will not allow direct streaming, I created an HttpHandler under my ASP.NET project to stream the file. My handler will read the file queried from the FTP using FtpWebRequest and send it back to the webpage. Following the code for my handler (GetAudio.ashx)
<%@ WebHandler Language="C#" Class="GetAudio" %>

using System;
using System.Web;
using System.Configuration;
using System.Net;
using System.IO;

public class GetAudio : IHttpHandler
{

    string ftpServer = ConfigurationManager.AppSettings["FTPServerForMRVPrompts"].ToString();
    string ftpUsername = ConfigurationManager.AppSettings["FTPServerForMRVPromptsUserName"].ToString();
    string ftpPassword = ConfigurationManager.AppSettings["FTPServerForMRVPromptsPassword"].ToString();

    public void ProcessRequest(HttpContext context)
    {
        string path = context.Request.QueryString["path"];
        string filePath = "ftp://" + ftpServer + path.ToString();
        try
        {
            var fineName = Path.GetFileName(filePath);
            FtpWebRequest ftpWebRequest = null;
            ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(filePath);
            // Get get the actual data            
            ftpWebRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
            ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpWebRequest.UsePassive = true;
            ftpWebRequest.UseBinary = true;
            ftpWebRequest.KeepAlive = false; //close the connection when done
                                           
            FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse();
            context.Response.Clear();
            context.Response.Buffer = true;
            context.Response.AddHeader("content-disposition",
                string.Format("application; filename=\"{0}\"", fineName));
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetMaxAge(new TimeSpan(DateTime.Now.AddMinutes(30).ToFileTime()));
            context.Response.ContentType = "audio/wav";

            byte[] buffer = new byte[1024];
            int bytes;
            Stream outStream = context.Response.OutputStream;
            using (Stream stream = response.GetResponseStream())
            {
                while ((bytes =
                    stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outStream.Write(buffer, 0, bytes);
                }
            }
        }
        catch
        {
           
        }
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
And in my web page, I am calling my handler as below

That's it.

Here I used Response caching in my handler with max-age of 30 mins, to enforce the file caching at browser.

Happy Coding 😊!

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment