Manish Rawat
Manish Rawat

Reputation: 1212

UrlActionResult in asp.net MVC

I want to know is there any open source action result code available for asp.net mvc in which i can pass a URL and it will response out the content.

Just like there are some inbuilt Action results

I want to build a custom Action Result in which i can pass a URL (basically an mp3 url) which will get downloaded in memory and the content will be streamed to the current executing response.

It should also support resume download if supported by server and client. Thats a must for me.

public UrlActionResult DownloadUrl(string url)
{
     return new UrlActionResult("http://www.example.com/audiofile.mp3");
}

Upvotes: 1

Views: 222

Answers (2)

Mouk
Mouk

Reputation: 1737

If you don't need to hide the url just use a redirect. Using a redirect with spare you server bandwidth:

public ActionResult DownloadUrl(string url)
{
     return new Redirect("http://www.example.com/audiofile.mp3");
}

Upvotes: 0

shenku
shenku

Reputation: 12448

For a basic scenario you could use something like this:

public class DownloadResult : ActionResult
{
    private readonly FileStreamResult _fileStreamResult;

    public DownloadResult(string url, string contentType)
    {
        using (var myWebClient = new WebClient())
        {
            var myStream = myWebClient.OpenRead(url);
            _fileStreamResult = new FileStreamResult(myStream, contentType);
        }
    }

    public override void ExecuteResult(ControllerContext context)
    {
        _fileStreamResult.ExecuteResult(context);
    }
}

However if you want to do the resume download it becomes a lot more complicated. Here is a great article with example code.

Upvotes: 1

Related Questions