【发布时间】:2013-10-31 07:12:41
【问题描述】:
我使用 ExtJS 4.2。我想在浏览文件时更改文件字段的值。这样做的原因是删除“C:\fakepath”字符串。任何帮助表示赞赏。
【问题讨论】:
-
你想保存在数据库端还是在ui端显示正确的值?
-
@Riku 我只想在文件字段中显示文件名。
我使用 ExtJS 4.2。我想在浏览文件时更改文件字段的值。这样做的原因是删除“C:\fakepath”字符串。任何帮助表示赞赏。
【问题讨论】:
这个C:\fakepath来自浏览器,所以你看不到真实的路径,但是可以隐藏路径,只显示文件名。您可以通过扩展文件字段来做到这一点:
Ext.define('Ext.form.field.ExtFile', {
extend: 'Ext.form.field.File',
onFileChange: function(button, e, value) {
var newValue = value.replace(/^c:\\fakepath\\/i, ''); // remove fakepath
return this.callParent([ button, e, newValue ]);
}
});
【讨论】:
onFileChange 函数不采用 button 参数。我将其替换为filefield。然后点击浏览按钮出现JS错误:Cannot call method 'apply' of undefined
Cannot call method 'apply' of undefined 错误表示未找到某些方法(例如在callParent 中使用apply)。您可以尝试拨打Ext.form.field.ExtFile.superclass.onFileChange.apply(this, [ button, e, newValue ]) 而不是this.callParent([ button, e, newValue ])
我发现最好的方法是覆盖该字段。
这是 ExtJs 4 和 5 的解决方案(在 ExtJs 6 上也适用于我):http://code.tonytuan.org/2014/10/extjs-get-rid-of-fake-path-in-file-field.html
Ext.define('Ext.enhance.form.field.File', {
override: 'Ext.form.field.File',
onFileChange: function(button, e, value) {
this.duringFileSelect = true;
Ext.form.field.File.superclass.setValue.call(this, value.replace(/^.*(\\|\/|\:)/, ''));
delete this.duringFileSelect;
}
});
【讨论】: