keepoffthegrass
03-11-2006, 03:31 PM
I have written an HttpHandler that intercepts requests for MP3s on my website - it logs the number of requests made for a particular file (I have included the ProcessRequest method below)
I have a flash mp3 player that then requests this file. Again this works well. However, if the user trys to navigate away from the website page containing the flash player during a request for an mp3, they cant until the whole mp3 has loaded.
This is not an issue with the player - I have confirmed this by requesting an mp3 with the httphandler switched off.
I think all the bandwidth for the particular browser windows assigned port is being used up by the downloading mp3 when the http handler is switched on.
Has anybody else experienced this issue. Is there a way around this problem?
You thoughts?
#region Implementation of IHttpHandler
public void ProcessRequest(System.Web.HttpContext context)
{
HttpRequest objRequest = context.Request;
HttpResponse objResponse = context.Response ;
string path = objRequest.Path.ToString();
int tempIndex = path.LastIndexOf("/");
string filename = path.Substring(tempIndex+1,path.Length -tempIndex-1);
try
{
//Remove the extension
string trackName = filename.Substring(0,filename.IndexOf("."));
//See if we can find the Track
Track requestedTrack = Track.GetTrack(new Guid(trackName));
if(requestedTrack!=null)
{
Play thisPlay = Play.NewPlay();
thisPlay.TrackID=requestedTrack.ID;
thisPlay.IP = context.Request.UserHostAddress;
requestedTrack.Plays.Add(thisPlay);
requestedTrack.Save();
//STREAMING V3
//This works in all browsers BUT prevents the user from browsing elesewhere within the site
string filePath = context.Server.MapPath("~") + "/Audio/UploadedAudio/" + filename;
FileStream oFile = new FileStream (filePath, FileMode.Open, FileAccess.Read);
try
{
objResponse.Buffer=true;
//New bits
objResponse.Clear();
objResponse.ClearContent();
objResponse.ClearHeaders();
//Fix 10th March - Updated Mime type data
objResponse.ContentType = "audio/x-mpeg";
context.Response.AddHeader("content-disposition",
"attachement; filename=" + filename);
context.Response.AddHeader("content-length",
oFile.Length.ToString());
BinaryReader br = new BinaryReader(oFile);
long filePtr=0;
//int maxBufferSize=10240;
int maxBufferSize=2048;
int bufferSize=maxBufferSize;
long fileSize = oFile.Length;
byte[] buffer=new byte[maxBufferSize];
while (filePtr<fileSize)
{
//determine if we have reached the end of the file
if (fileSize<filePtr+bufferSize)
{
bufferSize=(int)(fileSize-filePtr);
}
else
{
bufferSize=maxBufferSize;
}
//br.Read(buffer,filePtr,bufferSize);
buffer = br.ReadBytes(bufferSize);
for (int i=0; i<bufferSize; i++)
{
if(objResponse.IsClientConnected==true)
{
objResponse.OutputStream.WriteByte(buffer[i]);
}
}
objResponse.Flush();
filePtr+=bufferSize;
}
}
finally
{
oFile.Close();
}
}
else
{
objResponse.StatusCode = 404;
}
}
catch (Exception ex)
{
objResponse.StatusCode = 404;
}
}
I have a flash mp3 player that then requests this file. Again this works well. However, if the user trys to navigate away from the website page containing the flash player during a request for an mp3, they cant until the whole mp3 has loaded.
This is not an issue with the player - I have confirmed this by requesting an mp3 with the httphandler switched off.
I think all the bandwidth for the particular browser windows assigned port is being used up by the downloading mp3 when the http handler is switched on.
Has anybody else experienced this issue. Is there a way around this problem?
You thoughts?
#region Implementation of IHttpHandler
public void ProcessRequest(System.Web.HttpContext context)
{
HttpRequest objRequest = context.Request;
HttpResponse objResponse = context.Response ;
string path = objRequest.Path.ToString();
int tempIndex = path.LastIndexOf("/");
string filename = path.Substring(tempIndex+1,path.Length -tempIndex-1);
try
{
//Remove the extension
string trackName = filename.Substring(0,filename.IndexOf("."));
//See if we can find the Track
Track requestedTrack = Track.GetTrack(new Guid(trackName));
if(requestedTrack!=null)
{
Play thisPlay = Play.NewPlay();
thisPlay.TrackID=requestedTrack.ID;
thisPlay.IP = context.Request.UserHostAddress;
requestedTrack.Plays.Add(thisPlay);
requestedTrack.Save();
//STREAMING V3
//This works in all browsers BUT prevents the user from browsing elesewhere within the site
string filePath = context.Server.MapPath("~") + "/Audio/UploadedAudio/" + filename;
FileStream oFile = new FileStream (filePath, FileMode.Open, FileAccess.Read);
try
{
objResponse.Buffer=true;
//New bits
objResponse.Clear();
objResponse.ClearContent();
objResponse.ClearHeaders();
//Fix 10th March - Updated Mime type data
objResponse.ContentType = "audio/x-mpeg";
context.Response.AddHeader("content-disposition",
"attachement; filename=" + filename);
context.Response.AddHeader("content-length",
oFile.Length.ToString());
BinaryReader br = new BinaryReader(oFile);
long filePtr=0;
//int maxBufferSize=10240;
int maxBufferSize=2048;
int bufferSize=maxBufferSize;
long fileSize = oFile.Length;
byte[] buffer=new byte[maxBufferSize];
while (filePtr<fileSize)
{
//determine if we have reached the end of the file
if (fileSize<filePtr+bufferSize)
{
bufferSize=(int)(fileSize-filePtr);
}
else
{
bufferSize=maxBufferSize;
}
//br.Read(buffer,filePtr,bufferSize);
buffer = br.ReadBytes(bufferSize);
for (int i=0; i<bufferSize; i++)
{
if(objResponse.IsClientConnected==true)
{
objResponse.OutputStream.WriteByte(buffer[i]);
}
}
objResponse.Flush();
filePtr+=bufferSize;
}
}
finally
{
oFile.Close();
}
}
else
{
objResponse.StatusCode = 404;
}
}
catch (Exception ex)
{
objResponse.StatusCode = 404;
}
}