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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code for Downloading MP3 files

Status
Not open for further replies.

Salibas

Programmer
Feb 4, 2002
23
0
0
DE
I am making a website that downloads MP3 files (Legal ones ;-) )

Does anyone have an idea how to do it professionally. I mean, I don't want to make a link like
Code:
<a href=&quot;folder/mymusic.mp3&quot;>My Music</a>
because, obviously, it creates lots of unwanted results, likw playing the MP3 instead of downloading it.

I got a code in C#, but I dont have a .NET framework. How can I do it in ASP .

here is the C# code I got, it may give you a hint. I is for downloading PDF files:
Code:
<%@ Page Language=&quot;C#&quot; %>
<%@ Import Namespace=&quot;System.IO&quot; %>
<HTML>
<HEAD>
<script runat=&quot;server&quot; ID=Script1>
void Page_Load(object sender, System.EventArgs e) {
if (Page.IsPostBack){
FileStream MyFileStream = new FileStream(@&quot;c:\samerhtml\test\sample.pdf&quot;, FileMode.Open);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType=&quot;application/pdf&quot;;
Response.AddHeader( &quot;content-disposition&quot;,&quot;attachment; filename=MyPDF.PDF&quot;);
Response.BinaryWrite(Buffer);
}
}
</script>
</HEAD>
<body>
<form runat=&quot;server&quot;>
<asp:button id=&quot;link1&quot; Text = &quot;get PDF&quot; runat=&quot;server&quot; />
</form>
</body>
</HTML>
 
Whats stopping you from using the .NET framework and using the C# code?

Jack
 
'you'll need MSXML installed for this
<%

'get filename to upload
'it can be passed or pull it from db based on id
fl = Request.QueryString(&quot;fl&quot;)
path=Request.ServerVariables(&quot;SERVER_NAME&quot;)

'create MSXML object
Set xml = Server.CreateObject(&quot;MSXML2.XMLHTTP&quot;)

'get your mp3 file
xml.Open &quot;GET&quot;, &quot; & path & &quot;/mp3s/&quot; & fl , false
xml.Send



Response.ExpiresAbsolute = Now() - 1
Response.Expires = 0
Response.Buffer = TRUE

'play with this(zip) part.&quot;zip&quot; forces download.
Response.ContentType = &quot;application/zip&quot;

Response.AddHeader &quot;Content-Disposition&quot;, &quot;attachment;filename=&quot; & fl
Response.AddHeader &quot;Content-Length&quot;, lenb(xml.ResponseBody)

'start uploading to user
Response.BinaryWrite xml.ResponseBody

set rs=nothing
set xml = nothing


%>
'hope it works for you. I used it with PDF docs zipped into file and worked fine
 
Jack:

Thank you Jack, but actually the server I am using to host my page does not have a .NET framework and I can't change that, else I have to change the hosting, and I am not planning to do this now.

just_a_guy :

Thank you, you were helpfull. But during this time, I found a great Component that is made specially for downloads.
 
no problem. though I checked link you gave and seems to me it's just for user to upload to the server. I thought you asked for other way around. Anyhow, there's a component that has MUCH reacher functionality.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top