使用Extjs上传文件

时间:2015-07-31 07:02:05

标签: javascript extjs

您好我已经编写了上传文件和插入数据库的代码,因此我需要获取上传文件的字节数。

这是我的代码:

{
  xtype: 'filefield',
  id: 'AttachData',
  name: 'file-path',
  emptyText: 'Upload the document...',
  margin: "15 0 0 0",
  buttonText: 'Browse',
  msgTarget: 'side',
  allowBlank: false,
  anchor: '100%',
  disabled: false
}, {
  xtype: 'button',
  text: 'Upload',
  margin: "15 0 0 10",
  handler: function() {
    var form = this.up('form').getForm();
    if (form.isValid()) {
      form.submit({
        url: '../UploadAttachment.aspx',
        headers: {
          'Content-type': 'application/json;charset=utf-8'
        },
        waitMsg: 'Uploading your file...',
        success: function(response, action) {
          isUploded = true;
          msg('Success', 'Processed file "' + action.result.file + '" on the server');
        },
        failure: function(response, action) {
          console.log(action);
          Ext.Msg.alert('Failed', response.message ? action.result.message : 'No response');
        }
      });
    }
  }
}, 

任何人都会帮助我这个概念。需要获取上传文件的字节。

1 个答案:

答案 0 :(得分:1)

当您的请求转到" UploadAttachment.aspx"页面,只需在那里创建一个函数来获取上传的文件详细信息,并使用URL指定函数名称。因此该功能可以轻松调用服务器端或aspx页面。

您可以使用以下代码获取上传文件的完整详细信息。

HttpPostedFile uploadFile = Context.Request.Files["file-path"];

现在您可以在uploadFile变量中获取有关上传文件的所有信息。

实施例。

uploadFile.InputStream
uploadFile.FileName
and all others.

Default.aspx(编码)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="Libraries/ext-all.js"></script>
    <script src="Libraries/ext-all-debug.js"></script>
    <link href="Libraries/ext-theme-crisp-all-debug.css" rel="stylesheet" />

    <script type="text/javascript">

        Ext.onReady(function () {
            var window = new Ext.Window({
                id: 'grdWindow',
                width: 400,
                items: [
                    {
                        xtype: 'form',
                        renderTo: Ext.getBody(),
                        items: [
                            {
                                xtype: 'filefield',
                                id: 'AttachData',
                                name: 'file-path',
                                emptyText: 'Upload the document...',
                                margin: "15 0 0 0",
                                buttonText: 'Browse1',
                                msgTarget: 'side',
                                allowBlank: false,
                                anchor: '100%',
                                disabled: false
                            }, {
                                xtype: 'button',
                                text: 'Upload',
                                handler: function () {
                                    var form = this.up('form').getForm();
                                    if (form.isValid()) {
                                        form.submit({
                                            url: 'Default.aspx',
                                            headers: {
                                                'Content-type': 'application/json;charset=utf-8'
                                            },
                                            waitMsg: 'Uploading your file...',
                                            success: function (response, action) {
                                                isUploded = true;
                                                msg('Success', 'Processed file "' + action.result.file + '" on the server');
                                            },
                                            failure: function (response, action) {
                                                console.log(action);
                                                Ext.Msg.alert('Failed', response.message ? action.result.message : 'No response');
                                            }
                                        });
                                    }
                                }
                            }
                        ]
                    }
                ]
            }).show();
        });

        </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

Default.aspx.cs(处理程序)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpPostedFile fileUpload =  HttpContext.Current.Request.Files["file-path"];
        if (fileUpload != null)
        { 
            //You can get here anything from fileUpload
        }
    }

}