Geeks With Blogs
Łukasz Kuryło's blog

Wrzuta.pl, this is a polish site similar to youtube. It allows to watch movies and listen to music, but does not allow direct downloading them to disk. In this post I would like to show how to achieve that using Ext JS and MVC.

Create a new ASP.NET MVC project and add there a Ext JS library. I have written here how to do that.

First step is create a form where user can paste the url.


Ext.onReady(function()
{
    Ext.QuickTips.init();
    var form = new Ext.form.FormPanel({
        frame: true,
        renderTo: 'form',
        width: 380,
        title: 'Insert a link',
        items: [{
            xtype: 'textfield',
            allowBlank: false,
            anchor: '100%',
            vtype: 'LinkValidate', //(1)
            hideLabel: true,
            name: 'link'
}],
            buttons: [{
                text: 'Download',
                handler: function()
                {
                    }
}]
            });



This is a simple form with only one TextField, where user can insert a specified link and one button for submitting the form to the server. This field has a custom validation (1):


            Ext.form.VTypes['LinkValidateVal'] = /^http:\/\/([A-Za-z0-9\.-]+)\.wrzuta\.pl\/([A-Za-z0-9\._\/-]+)$/;
            Ext.form.VTypes['LinkValidateText'] = 'Invalid link';
            Ext.form.VTypes['LinkValidate'] = function(arg)
            {
                return Ext.form.VTypes['LinkValidateVal'].test(arg);
            }


This validation check if the inserted link corresponds to the pattern of url for wrzuta.pl.
About custom validation I have written here.


Second step is create a controller with a Download action where the inserted link is converted to the absolute path to the file for download

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Download()
        {
            string link = Request.Form["link"];
            string login, file_key;
            string retString = "{success: false, error: \"Error. Probably the link is broken.\"}";
            if (!String.IsNullOrEmpty(link))
            {
                Regex re = new Regex(@"^http://([A-Za-z0-9-]+)\.wrzuta\.pl/(audio|film)/([A-Za-z0-9]+)/([A-Za-z0-9\._-]+)$");
                Match match = re.Match(link);
                login = match.Groups[1].Value;
                file_key = match.Groups[3].Value;

                if (!String.IsNullOrEmpty(login) && !String.IsNullOrEmpty(file_key))
                {

                    link = "http://" + login + ".wrzuta.pl/sr/f/" + file_key;

                    retString = "{success:true, url:\"" + link + "\"}";

                    return Content(retString);
                }
            }

            return Content(retString);
        }


ASP.NET MVC provides a RedirectResult, but Ext needs a response in JSON format, so the ContentResult is used and the redirect to the link is placed in the form handler:

            handler: function()
                {
                    if (form.getForm().isValid())
                    {
                        form.getForm().submit({
                            url: 'Wrzuta/Download',
                            success: function(a, b)
                            {
                                window.location = b.result.url;
                            },
                            failure: function(a, b)
                            {
                                Ext.Msg.alert('Result', b.result.error);
                            }
                        });
                    }
                }


Full source code is available here (click the blue button called "Pobierz plik").


Posted on Sunday, September 27, 2009 9:20 PM ASP.NET MVC , Ext JS | Back to top


Comments on this post: Downloading a file from wrzuta.pl

Comments are closed.
Comments have been closed on this topic.
Copyright © Łukasz Kuryło | Powered by: GeeksWithBlogs.net