WAV File not downloading properly

  • Thread starter Thread starter nateastle
  • Start date Start date
N

nateastle

I have a application that stores a wav file into the database. I
retrieve the wav file through a web service call that gets the file.
When I try to send the file through the response stream it seems the
file gets changed. If I save the file to disk it works properly.

Here is my latest endeavor

Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" +
"test.wav");
BinaryWriter bw = new BinaryWriter(Response.OutputStream);
bw.Write(objPromptStorageOut.promptwave);
bw.Close();
Response.ContentType = ATACUtility.getMimeFromFile
(objPromptStorageOut.promptwave);
Response.End();

The file that gets downloaded is 2 times the size of the original
file.

Here is how I write it to disk
using (BinaryWriter binWriter = new BinaryWriter(File.Open(@"C:
\nater.wav", FileMode.Create)))
{

binWriter.Write(objPromptStorageOut.promptwave);
}

that currently works.
 
Use ratherhttp://msdn.microsoft.com/en-us/library/system.web.httpresponse..binar...

Keep in mind that the Response outputstream is intended to write
"characters" and applies the encoding defined in your web.config file.

Response.BinaryWrite will write the bytes without any encoding...

Thank you for your reply. I have tried your suggestion and it still
did not work.

Here is what I tried:

Response.AddHeader("Content-Disposition", "attachment; filename=" +
"nate.wav");
Response.BinaryWrite(objPromptStorageOut.promptwave);
Response.Flush();
Response.End();

and that generated a file of 155,365 bytes when the original file was
80,120
if I add

Response.ContentType = "audio/wav";
Response.AddHeader("Content-Length",
(objPromptStorageOut.promptwave.Length).ToString());

then I get the correct file size but the file is unplayable.
 
Could it be a similar encoding problem when you read the file ? How do you
read the file in objPromptStorageOut.PromptWave ?

My approach would be to validate each step that is :
- to start by streaming a well known byte array (let's take 16 bytes such as
0x00 up to 0xf0). You'll easily see if it works and moreover if it doesn't
work you'll be able to quickly check what changed in the content.
- if it works read the same content from a file. If it doesn't work you'll
know you have a problem in how you read the file.
- then once that work, it should work with the actual file.
 
Could it be a similar encoding problem when you read the file ? How do you
read the file in objPromptStorageOut.PromptWave ?

My approach would be to validate each step that is :
- to start by streaming a well known byte array (let's take 16 bytes suchas
0x00 up to 0xf0). You'll easily see if it works and moreover if it doesn't
work you'll be able to quickly check what changed in the content.
- if it works read the same content from a file. If it doesn't work you'll
know you have a problem in how you read the file.
- then once that work, it should work with the actual file.

The file is stored in blob column in an oracle database, I have a web
service that returns the byte array back. If I write the file directly
to the disk in the web service it plays, if I write it directly to the
disk on the website it works, the only thing that doesn't work is when
I try to send the file in the response. When I look at the first bytes
of the file, I get the correct header for the wav file. I will try
with a smaller file and see where it is going wrong.

Nate
 
The file is stored in blob column in an oracle database, I have a web
service that returns the byte array back. If I write the file directly
to the disk in the web service it plays, if I write it directly to the
disk on the website it works, the only thing that doesn't work is when
I try to send the file in the response. When I look at the first bytes
of the file, I get the correct header for the wav file. I will try
with a smaller file and see where it is going wrong.

Nate

Can it be a problem with authorization?
http://forums.asp.net/p/1243133/2276691.aspx#2276691

If it doesn't help try to set "application/x-msdownload" instead if
"audio/wav", this is a special type that should prompt the user with
an open-or-save dialog.

Response.AddHeader("content-disposition", "attachment;
filename=test.wav")
Response.ContentType = "application/x-msdownload"
Response.BinaryWrite...
 
Can it be a problem with authorization?http://forums.asp.net/p/1243133/2276691.aspx#2276691

If it doesn't help try to set "application/x-msdownload" instead if
"audio/wav", this is a special type that should prompt the user with
an open-or-save dialog.

Response.AddHeader("content-disposition", "attachment;
filename=test.wav")
Response.ContentType = "application/x-msdownload"
Response.BinaryWrite...

I created a test project with only the hyper link and the same calls
and that seemed to work. I am going to say it has something to do with
the security or something else on the page that is changing the
response. I tried changing the file to grant all users access to the
aspx file and that did not work.
 
I created a test project with only the hyper link and the same calls
and that seemed to work. I am going to say it has something to do with
the security or something else on the page that is changing the
response. I tried changing the file to grant all users access to the
aspx file and that did not work.

Looks a tough one. Now I would try to use a blob wiht those 16 values 0x0 to
0xf0 and see how it behaves. Save the file on disk and use an hexa file
viewer to see what was changed in the file...

Good luck.
 
I created a test project with only the hyper link and the same calls
and that seemed to work. I am going to say it has something to do with
the security or something else on the page that is changing the
response. I tried changing the file to grant all users access to the
aspx file and that did not work.- Hide quoted text -

- Show quoted text -

One thing I forgot to mention is that the ClearContent method does not
clear header information. If you do not use Clear or ClearHeaders
before calling ClearContent then add Response.ClearHeaders() to ensure
that no other headers are sent with the current response.
 
Back
Top