Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi there,

I have a requirement where i have the url of an mp3 file (ex: http://abc.com/records/ac_12.mp3)
and i want this file to be downloaded to the client machine whenever he clicks to download.

I have tried some way using webClient. But it was downloading and saving to server only.
But i want it to be downloaded to Client machine.

code i used is:

C#
String FileName = "FileName.mp3";
String FilePath = "https:/api.twilioxxx.com/2010-04-01xxx/AccountSidxxxx/Residxxx.mp3";//Not real Path
WebClient client = new WebClient();
byte[] data = client.DownloadData(new Uri(url));

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();

response.ContentType = "audio/mpeg";

response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", "aspnet.mp3")); //filename=" + FileName + ";");
//response.OutputStream.Write(data, 0, data.Length);
//response.TransmitFile(FilePath);
response.BinaryWrite(data);
//response.Flush();
response.End();


Here i have commented some of the things i tried with..
thank you in advance...
Posted
Updated 12-Dec-13 0:11am
v4
Comments
DaveAuld 12-Dec-13 3:30am    
What exactly are you trying to do? I'm confused when you started talking clients/servers etc. Is the Client a webpage or an application etc. The server an IIS app or standalone server app etc. update and clarify in your question.
Vikas Kottari 12-Dec-13 4:44am    
I have a web application hosted in IIS.
wen customer logs in, i will display some mp3 files which are from external path like https://api.xxx.com/2010-04-01/AccountsId/Recording.mp3 and they are not stored in my DB.
Whenever the customer clicks to download this file, it should get downloaded to his PC.

If you are passing the link to the client for presentation on their browser, simply embedded the url on the page as a clickable link. This will allow them to click the file and download it without any intervention from yourself. Just like they would if visiting any site and try to download a file e.g. visiting a article on codeproject and downloading the source demo.
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;

namespace example
{
    /// <summary>
    /// Summary description for intakeDownload
    /// </summary>
    public class downlaod: IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            // Get the file name from the query string
            string queryFile = context.Request.QueryString["file"];

            // Ensure that we were passed a file name
            if (string.IsNullOrEmpty(queryFile))
                throw new HttpException(404, null);

            // Generate the server file name
            string file = Path.Combine(ConfigurationManager.AppSettings["upload_path"], queryFile);

            // Ensure that the file exists
            if (!File.Exists(file))
            {
                throw new HttpException(404, null);
            }
            else
            {
                //context.Response.ContentType = "image/jpeg";
                context.Response.ContentType = "application/octet-stream";

                // Set the filename
                context.Response.AddHeader("content-disposition", "attachment;filename=" + queryFile);

                // Stream the file to the client
                context.Response.WriteFile(file);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900