关于extjs4.0中表单上传

      做着做着东西,遇到了两个问题,一个就是表单上传,还有一个是解析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  解决,。菜鸟就是菜鸟,没经验啊~~~~这么多弯路,曲曲折折啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值