EXTJS 4 UPLOAD TO ASP.NET MVC DEMO

Note: server response type should be "text/html".

RESULT

204323_t1BH_200350.png

 

C# CODE

[HttpPost]
public ActionResult File()
{
    if (Request.Files.Count == 1)
    {
        var file = Request.Files["file"];
        if (file != null)
        {
            var size = file.ContentLength;
            var name = System.IO.Path.GetFileName(file.FileName);
 
            if (size > 0)
            {
                if (size <= 2 * 1024 * 1024)
                {
                    return Json(new
                        {
                            success = true,
                            msg = "Your file has been uploaded",
                            data = new
                                {
                                    name, size
                                }
                        }, "text/html");
                }
 
                return Json(new
                    {
                        success = false,
                        msg = "Your file is too large"
                    }, "text/html");
            }
        }
    }
 
    return Json(new
    {
        success = false,
        msg = "Select file to upload"
    }, "text/html");
}

JS CODE 

Ext.onReady(function () {
    Ext.widget('form', {
        title: 'Upload Demo',
        width: 400,
        bodyPadding: 10,
        items: [{
            xtype: 'filefield',
            name: 'file',
            fieldLabel: 'File',
            labelWidth: 50,
            anchor: '100%',
            buttonText: 'Select File...'
        }],
        buttons: [{
            text: 'Upload',
            handler: function () {
                var form = this.up('form').getForm();
                if (form.isValid()) {
                    form.submit({
                        url: '/upload/file',
                        waitMsg: 'Uploading your file...',
                        success: function (f, a) {
                            var result = a.result,
                                data = result.data,
                                name = data.name,
                                size = data.size,
                                message = Ext.String.format('<b>Message:</b> {0}<br>' +
                                    '<b>FileName:</b> {1}<br>' +
                                    '<b>FileSize:</b> {2} bytes',
                                    result.msg, name, size);
 
                            Ext.Msg.alert('Success', message);
                        },
                        failure: function (f, a) {
                            Ext.Msg.alert('Failure', a.result.msg);
                        }
                    });
                }
            }
        }],
        renderTo: Ext.getBody()
    });
});

 

转载于:https://my.oschina.net/u/200350/blog/1590913

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值