日期:2014-05-16  浏览次数:20380 次

Extjs中Firefox获取上传文件的全路径


function readFileFirefox(fileBrowser) {
??? try {
??? ??? netscape.security.PrivilegeManager
??? ??? ??? ??? .enablePrivilege("UniversalXPConnect");
??? } catch (e) {
??? ??? alert('路径错误!');
??? ??? return;
??? }

??? var fileName = fileBrowser.value;
??? var file = Components.classes["@mozilla.org/file/local;1"]
??? ??? ??? .createInstance(Components.interfaces.nsILocalFile);
??? try {
??? ??? file.initWithPath(fileName.replace(/\//g, "\\\\"));
??? } catch (e) {
??? ??? if (e.result != Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH)
??? ??? ??? alert('路径错误!');
??? ??? return;
??? }

??? if (file.exists() == false) {
??? ??? alert("File '" + fileName + "' not found.");
??? ??? return;
??? }
??? return file.path;
}

var form = new Ext.form.FormPanel({
??? ??? id : 'form',
??? ??? bodyStyle : itemPadding,
??? ??? title : '上传文件',
??? ??? header : true,
??? ??? fileUpload : true,
??? ??? height : 160,
??? ??? items : [{
??? ??? ??? xtype : 'textfield',
??? ??? ??? inputType : 'file',
??? ??? ??? fieldLabel : '上传文件',
??? ??? ??? id : 'uploadFileFieldPath',
??? ??? ??? allowBlank : false,
??? ??? ??? blankText : '请输入要导入数据完整路径'
??? ??? }],
??? ??? buttons : [{
??? ??? ??? id : 'importBtn',
??? ??? ??? text : '导入',
??? ??? ??? handler : function() {
??? ??? ??? ??? if (form.form.isValid()) {
??? ??? ??? ??? ??? var filePath = Ext.getCmp('uploadFileFieldPath').getValue();
??? ??? ??? ??? ??? if ((navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent
??? ??? ??? ??? ??? ??? ??? .indexOf("Mozilla") != -1)
??? ??? ??? ??? ??? ??? ??? && (!Ext.isIE)) {
??? ??? ??? ??? ??? ??? filePath = readFileFirefox(document
??? ??? ??? ??? ??? ??? ??? ??? .getElementById('uploadFileFieldPath'));
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? form.form.submit({
??? ??? ??? ??? ??? ??? url : 'saveLogImportAjax.action',
??? ??? ??? ??? ??? ??? params : {
??? ??? ??? ??? ??? ??? ??? realFilePath : filePath
??? ??? ??? ??? ??? ??? },
??? ??? ??? ??? ??? ??? success : function(form, action) {
??? ??? ??? ??? ??? ??? ??? Ext.Msg.alert('提示', action.result.resultString);
??? ??? ??? ??? ??? ??? },
??? ??? ??? ??? ??? ??? failure : function(form, action) {
??? ??? ??? ??? ??? ??? ??? Ext.Msg.alert('提示', action.result.resultString);
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? });
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }]
});

传入到action的realFilePath就是上传文件的绝对路径了。网上找了好久才找到的,记录一下。