带有extjs 4.2的文件字段没有伪路径

时间:2014-03-10 14:24:19

标签: javascript extjs

我想要extjs 4.2

我在附件中使用此组件:

{
    xtype: 'filefield',
    id: 'file6',
    fieldLabel: 'test ',
    labelWidth: 100,
    msgTarget: 'side',                  
    allowBlank : false,
    anchor: '100%',
    buttonText: 'upload'
},

我希望有一个附件组件,显示没有此文本的文件名:   c / fakepath

3 个答案:

答案 0 :(得分:10)

没有内置的方法来实现这一点,但是,您可以执行查找/替换伪路径并删除。我在change事件上强调了这一点。这是一个例子:

listeners: {
                        change: function(fld, value) {
                            var newValue = value.replace(/C:\\fakepath\\/g, '');
                            fld.setRawValue(newValue);
                        }
                    }

我创建了一个展示工作示例的sencha fiddle

答案 1 :(得分:2)

与user3550464的答案类似,但代码却少了很多:

将此添加到文件字段侦听器对象。 此代码将删除所有文本,包括字段中的最后一个斜杠或反斜杠。

change: function (field, value) {
    'use strict';
    var newValue = value.replace(/(^.*(\\|\/))?/, "");
    field.setRawValue(newValue);
}

答案 2 :(得分:0)

这是基于weekdev的回答的改进。 将以下侦听器添加到文件字段:

/* remove the path (if any) so that only the file name is displayed
 * note: to be consistent across browsers
 * -- some of them give a fake path, some others give a file name
 */
change: function(fileField, value, eventOptions) {
    var newValue;
    var lastForwardSlash = value.lastIndexOf('\/');
    /* if this is a Windows-based path or just a file name
     * (Windows-based paths and file names in general don't contain forward slashes)
     */
    if (lastForwardSlash < 0) {
        /* remove the characters before the last backslash (included)
         * but only for Windows users -- as UNIX-like file names may contain backslashes
         */
        if (Ext.isWindows === true) {
            newValue = value.substring(value.lastIndexOf('\\') + 1);
        } else {
            newValue = value;
        }
    }
    // there is a forward slash: this is a UNIX-like (Linux, MacOS) path
    else {
        // remove the characters before the last forward slash (included)
        newValue = value.substring(lastForwardSlash + 1);
    }
    fileField.setRawValue(newValue);
}