Force MP3 Download

  • Thread starter Thread starter Goldenrate
  • Start date Start date
G

Goldenrate

Hi Everyone,

I'm trying to force My application to download MP3 file instead of playing
it in browser.

I used this asp cod to create download image

/******************//
<asp:ImageButton ID="ibDownload" runat="server" style="border:0;"
ImageUrl="~/DesktopModules/ASPvia_AudioDownload/images/Download.jpg"
onclick="ibDownload_Click" />
</div>
/*********************/
When clicked it calls

/***********************/
protected void ibDownload_Click(object sender, ImageClickEventArgs e)
{
string f =
String.Concat("Portals\\0\\",GetUserIDForProductOwner(),"\\",GetMediaFileName());
string filePath = Server.MapPath(f);
Response.Clear();
Response.AddHeader("content-disposition", "attachment;
filename=" + GetMediaFileName());
Response.ContentType = "application/audio/mpeg";
Response.WriteFile(filePath);
Response.End();
}
/*******************************/
GetMediaFileName() - retrieve the file name from the database.
f - stores the virtual directory of the file.

I've checked the code time and time again and found nothing wrong.
Does anybody know what am I doing wrong?
Thanks,
 
Hello David,

I cannot see any wrong parts in the code. Actually, I also use your codes
and write a simple test project in my side. It works fine. When I click the
download button, the browser pops a File Download dialog to ask me to
download the mp3 file. You can get my project from my

http://cid-c2e0d62e8a095a30.skydrive.live.com/self.aspx/Public/WebApplicatio
nTest.zip

I think the issue may also be related to the Web browser. I test on both of
IE and FireFox. What browser are you using? And if you test with my
project, do you still encounter the same issue?


Best regards,
Ji Zhou
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
 
Hello goldenrate,
Hi Everyone,

I'm trying to force My application to download MP3 file instead of
playing it in browser.

I used this asp cod to create download image

/******************//
<asp:ImageButton ID="ibDownload" runat="server" style="border:0;"

ImageUrl="~/DesktopModules/ASPvia_AudioDownload/images/Download.jpg"
onclick="ibDownload_Click" />
</div>
/*********************/
When clicked it calls

/***********************/
protected void ibDownload_Click(object sender, ImageClickEventArgs
e)
{
string f =
String.Concat("Portals\\0\\",GetUserIDForProductOwner(),"\\",GetMediaF
ileName());
string filePath = Server.MapPath(f);
Response.Clear();
Response.AddHeader("content-disposition", "attachment;
filename=" + GetMediaFileName());
Response.ContentType = "application/audio/mpeg";
Response.WriteFile(filePath);
Response.End();
}
/*******************************/
GetMediaFileName() - retrieve the file name from the database. f -
stores the virtual directory of the file.

I've checked the code time and time again and found nothing wrong.
Does anybody know what am I doing wrong?
Thanks,

It may sound stupid, but replace your original ContentType with application/octet-stream
that should give the browser an additional hint, it ins't supposed to stream
the content to a player.
 
Thank you all for your help.

Actually I was able to solve it. The problem, like in so many other error,
is AJAX. I disabled it and it all work fine now.

Cheers,
:)
 
Back
Top