首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在JSP中使用上传文件,并在上传之前在type=file中执行一些检查...

在JSP中使用上传文件,并在上传之前在type=file中执行一些检查...
EN

Stack Overflow用户
提问于 2012-04-12 16:49:30
回答 2查看 845关注 0票数 1

目前我们有这个上传文件的代码

代码语言:javascript
复制
<input id='fileBrowse' type='file' style="width:187px;height:20px" class='fileBrowse' onchange="onBrowseFile( this );" />

执行一些检查的javascript函数如下所示:

代码语言:javascript
复制
    function onBrowseFile( fb ) {
    var myFSO = new ActiveXObject("Scripting.FileSystemObject");
    var thefile = myFSO.getFile(fb.value);
    if( (thefile.size / 1000000) > maxfilesize) {
        alert( "The size of the files you have tried to drag and drop exceed the maximum allowed. Please drag no more than "+maxfilesize+" MB at a time." );
        return;
    }
    if( fb.value.indexOf( ".exe" ) > -1 ||
            fb.value.indexOf( ".asp" ) > -1 ||
            fb.value.indexOf( ".aspx" ) > -1 ||
            fb.value.indexOf( ".cab" ) > -1 ||
            fb.value.indexOf( ".com" ) > -1 ||
            fb.value.indexOf( ".dll" ) > -1 ||
            fb.value.indexOf( ".java" ) > -1) {
        alert( "The import of one or more files type are not permitted" );
        return;
    }
    document.getElementById( "txtFilePath" ).value = fb.value;
}

现在的问题是,我们现在使用IE9,而IE9安全性不允许我们在不修改安全设置或注册表的情况下使用ActivexControl。我们不能这样做,因为我们有5000个用户使用这个应用程序。

请建议我们还可以用什么来解决这个问题。我们必须要有这些支票。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-12 16:58:34

您可以在服务器端运行检查。

票数 0
EN

Stack Overflow用户

发布于 2012-04-12 17:48:10

将HTML5用于IE9。

代码语言:javascript
复制
var upload = document.getElementById('fileBrowse');

upload.onchange = function (e) {
  e.preventDefault();
  var file = upload.files[0];
  if( ( file.fileSize) > (2 * 1024*1024) ) {
        alert( "The size of the files you have tried to drag and drop exceed the maximum allowed. Please drag no more than 1 MB at a time." );
        return;
  }
  if( file.name.indexOf( ".exe" ) > -1 ||
        file.name.indexOf( ".asp" ) > -1 ||
        file.name.indexOf( ".aspx" ) > -1 ||
        file.name.indexOf( ".cab" ) > -1 ||
        file.name.indexOf( ".com" ) > -1 ||
        file.name.indexOf( ".dll" ) > -1 ||
        file.name.indexOf( ".java" ) > -1 ) {
        alert( "The import of one or more files type are not permitted" );
        return;
  }
  return false;
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10120278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档