做着做着東西,遇到了兩個問題,一個就是表單上傳,還有一個是解析txt文檔,並把數據顯示到grid上。其實糾結到最後就是一個問題,上傳文件。、
IE8,9 firefox 都改變了安全機制,不再像ie6一樣,直接就可以獲得我所要上傳文件的全部路徑,ie8是在文本框中顯示“C:\fakepath\”+文件名及其後綴。firefox就乾脆給了個文件名和後綴。一開始我調試ext都用的是ie8,測試上傳的時候,就看到 “C:\fakepath\1.txt”這是個神馬路徑啊,而且獲取的時候也只有1.txt。果斷的認爲不對,果斷的去百度,果斷的找到原因,果斷的改了ie8的安全選項,唉。。。沒辦法 誰叫我是大菜鳥。
慢慢後來順風接着順水。換到firefox上的時候 出錯了,然後根據firefox的js規則改了又改,然後ie8上面不行了,頭說這不行啊,你得全不兼容啊,這像啥話啊。崩潰啊。我想想說這怎麼可能嘛,它們有點互相都不認識啊,怎麼做到兩個都可以用。拖了好久,看到一個技術貼。果斷的只寫了一句話。。。
if (Ext.isIE) {
Ext.enableGarbageCollector = false;
} 這個解決了兼容性。
迴歸正題。雖然我改了ie8的安全選項,可是火狐的沒有改,一運行,出錯了,上傳不了啊,然後就是各種百度。方法也找到了,同樣也是改安全選項
<script type="text/javascript">
function readFile(fileBrowser) {
if (navigator.userAgent.indexOf("MSIE")!=-1)
readFileIE(fileBrowser);
else if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Mozilla")!=-1)
readFileFirefox(fileBrowser);
else
alert("Not IE or Firefox (userAgent=" + navigator.userAgent + ")");
}
function readFileFirefox(fileBrowser) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e) {
alert('Unable to access local files due to browser security settings. To overcome this, follow these steps: (1) Enter "about:config" in the URL field; (2) Right click and select New->Boolean; (3) Enter "signed.applets.codebase_principal_support"
(without the quotes) as a new preference name; (4) Click OK and try loading the file again.');
return;
}
var fileName=fileBrowser.value;
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
try {
// Back slashes for windows
file.initWithPath( fileName.replace(/\//g, "\\\\") );
}
catch(e) {
if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e;
alert("File '" + fileName + "' cannot be loaded: relative paths are not allowed. Please provide an absolute path to this file.");
return;
}
if ( file.exists() == false ) {
alert("File '" + fileName + "' not found.");
return;
}
alert(file.path); // I test to get the local file's path.
var is = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance( Components.interfaces.nsIFileInputStream );
try { is.init( file,0x01, 00004, null); }
catch (e) {
if (e.result!=Components.results.NS_ERROR_FILE_ACCESS_DENIED) throw e;
alert("Unable to access local file '" + fileName + "' because of file permissions. Make sure the file and/or parent directories are readable.");
return;
}
var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance( Components.interfaces.nsIScriptableInputStream );
sis.init( is );
var data = sis.read( sis.available() );
alert("Data from file: " + data); // I test to get the local file's data.
}
function readFileIE(fileBrowser) {
var data;
try {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileName=fso.GetAbsolutePathName(fileBrowser.value);
if (!fso.FileExists(fileName)) {
alert("File '" + fileName + "' not found.");
return;
}
var file = fso.OpenTextFile(fileName, 1);
data = file.ReadAll();
alert("Data from file: " + data);
file.Close();
}
catch(e) {
if (e.number == -2146827859) {
// This is what we get if the browser's security settings forbid
// the use of the FileSystemObject ActiveX control
alert('Unable to access local files due to browser security settings. To overcome this, go to Tools->Internet Options->Security->Custom Level. Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to
"Enable" or "Prompt"');
}
else if (e.number == -2146828218) {
// This is what we get if the browser can't access the file
// because of file permissions
alert("Unable to access local file '" + fileName + "' because of file permissions. Make sure the file and/or parent directories are readable.");
}
else throw e;
}
}
</script>
當我屁顛屁顛的去找頭讓他看(我只改了我自己的機器),頭看了,說 這回行了。不過他要用自己機子再測試一回,我說測吧,果斷報錯。我就給他解釋爲什麼,告訴他怎麼改這個火狐的安全機制。他看着說,這不行啊,你不能讓用戶自己手動改吧,不能這麼幹的。我一想也是啊。。。我這搞了幾天,完全背離了。
我是不是把這個事情想的太複雜了點,就是一個上傳文件啊,哪裏搞出那麼多事情啊。然後刪除了這些代碼,我用的是struts2+extjs,我想我完全可以試着用struts的上傳,我爲什麼非要先獲取到路徑啊,而且這個機制我現在也是獲取不到路徑的。先上傳,然後我在用,這樣的路徑就是服務器路徑,而不是我本機路徑,這個應該行,
js:
items : {
xtype : 'filefield',
id : 'photo',
name : 'photo',
anchor : '90%',
buttonText : '上傳附件',
buttonOnly : true,
listeners : {
'change' : function(fb, v) {
noticeinfo.form.submit( {
clientValidation : true,
url : 'uplinshifile.action',
method : 'POST',
success : function(form, action) {
var r = Ext.ModelManager.create( {
FILENAME : v,
FILEPATH : v,
ACTION : '附件刪除'
}, 'File');
fstore.insert(0, r);
},
failure : function(form, action) {
}
});
}
}
}
action:
private File photo;
private String photoContentType;
private String photoFileName;
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
// 把附件上傳到臨時文件夾
@SuppressWarnings("unchecked")
public String upLinShiFile() throws Exception {
String path=up.uploadOneFile(getPhoto(), getPhotoFileName(), "/tmp");
if(ActionContext.getContext().getSession().get("map")==null){
HashMap<String, String> map=new HashMap<String, String>();
map.put(getPhotoFileName(), path);
ActionContext.getContext().getSession().put("map", map);
}else{
HashMap<String, String> map = (HashMap<String, String>)ActionContext.getContext().getSession().get("map");
map.put(getPhotoFileName(), path);
ActionContext.getContext().getSession().put("map", map);
}
return null;
}
OK 解決,。菜鳥就是菜鳥,沒經驗啊~~~~這麼多彎路,曲曲折折啊