Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending files to the browser

Status
Not open for further replies.

RicksAtWork

Programmer
Nov 1, 2005
120
GB
I am trying to send an mp3 to a browser. However, it takes about 20 seconds before anything appears, and then file appears all of a sudden in blocks.

How can I make teh app so the response is sent immediately in chunks?

This is my code so far:

FileStream oFile = new FileStream(context.Server.MapPath("~") + "/Audio/UploadedAudio/" + filename, FileMode.Open);
objResponse.Charset = "";
objResponse.ContentType = "audio/mpeg3";


BinaryReader br = new BinaryReader(oFile);
for (long l = 0; l < oFile.Length; l++)
{
objResponse.OutputStream.WriteByte(br.ReadByte());

}

oFile.Close();

 
This is my second attempt - this seems to kill my server completely....

Any ideas where I'm going wrong??

string filePath = context.Server.MapPath("~") + "/Audio/UploadedAudio/" + filename;

FileStream oFile = new FileStream (filePath, FileMode.Open, FileAccess.Read);

try
{
objResponse.Buffer=false;

BinaryReader br = new BinaryReader(oFile);

long filePtr=0;
int maxBufferSize=1024;
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++)
{
objResponse.OutputStream.WriteByte(buffer);

}


filePtr+=bufferSize;
}


}
finally
{
oFile.Close();
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top