Filefield with extjs 4.2 without fakepath

血红的双手。 提交于 2019-12-18 06:56:20

问题


I want with extjs 4.2

I use this component in attachment :

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

I want to have a attachment component which display name of file without this text : c /fakepath


回答1:


There isn't a built-in way to accomplish this however, you can do a find/replace for fakepath and remove. I impelmented this on the change event. Here is an example:

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

I created a sencha fiddle demonstrating a working example




回答2:


Similar to user3550464's answer, but in a whole lot less code:

Add this to your file field listeners object. This code will remove all text up to and including the last slash or backslash in the field.

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



回答3:


Here is an improvement based on weeksdev's answer. Add the following listener to your file field:

/* 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);
}


来源:https://stackoverflow.com/questions/22302841/filefield-with-extjs-4-2-without-fakepath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!