javascript - 使用Extjs上传文件

标签 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');
        }
      });
    }
  }
}, 

任何人都可以帮助我理解这个概念。需要获取上传文件的字节数。

最佳答案

当您的请求转到“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
        }
    }

}

关于javascript - 使用Extjs上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31739967/

相关文章:

javascript - D3js 移动嵌套组

javascript - 通过索引获取值和引用对象属性

javascript - Controller 如何在 Extjs 应用程序中与 MVC 一起使用

extjs - 将表单内的网格扩展到全高

javascript - 如果我传递 "this"中的函数,监听器将不起作用

javascript - 无法从 RSS feed 获取所有信息

javascript - Google Map API BackBoneJS 无法读取 null 的属性 'offsetWidth'

javascript - LoopBack操作钩子(Hook)使用多种模型

javascript - 对具有点分隔数字的属性的对象数组进行排序

extjs - 将此分配给没有回调的语言环境变量的原因是什么?