首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

控制上传文件的大小的有关问题

2012-01-18 
控制上传文件的大小的问题?我在后台写 this.file1.PostedFile.ContentLength 10000000用来控制上传文件

控制上传文件的大小的问题?
我在后台写 this.file1.PostedFile.ContentLength < 10000000 用来控制上传文件大小 可是这种方法是在我把文件上传到服务器后 才开始判断大小的 文件小的还可以判断 当文件比较大的时候 页面会死 这种情况怎么解决阿 能不能在没有把文件上传到服务器前的时候 就判断大小阿 或者传到服务器后再判断文件大小 如果超过限制 再删除它 可是这种情况页面怎么会不死啊
 我也上网搜了很多 可是回复都是讲大框的 希望能提供具体的方法

[解决办法]
上传前判断要用到activex,否则你是没权限判断的.参考:

HTML code
<HTML><HEAD>               <SCRIPT>                               function       getFileSize       (fileName)       {                               if       (document.all)       {               window.oldOnError       =       window.onerror;                               window.onerror       =       function       (err)       {               if       (err.indexOf('utomation')       !=       -1)       {               alert('没有访问文件的权限');                                               return       true;                                       }                                   else                   return       false;                               };                               var       fso       =       new       ActiveXObject('Scripting.FileSystemObject');                               var       file       =       fso.GetFile(fileName);                               window.onerror       =       window.oldOnError;                               return       file.Size;           }}               </SCRIPT>               </HEAD>               <BODY>               <FORM       NAME="formName"><INPUT       TYPE="file"       NAME="fileName"><BR>               <INPUT       TYPE="button"       VALUE="查看文件字节大小"               ONCLICK="alert(getFileSize(this.form.fileName.value))"><BR>               </FORM></BODY>               </HTML>
[解决办法]
IE都话 可以这样
JScript code
function getFileSize(filePath)   {      var fso = new ActiveXObject("Scripting.FileSystemObject");      return fso.GetFile(filePath).size;   }
[解决办法]
图片的话 可以这样
JScript code
function getFileSize(filePath){   var image=new Image();   image.dynsrc=filePath;   return image.fileSize;}
[解决办法]
web.config内:

<httpRuntime executionTimeout="300" maxRequestLength="10240"/>
</system.web>

executionTimeout设置超时秒数
maxRequestLength允许大小:kb

[解决办法]
下面的代码供你参考一下,你可以自己改成aspx页面里能用的方式

<html>
<head>
<title>客户端控制上传图像大小</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
var FileMaxSize = 50;//限制上传的文件大小,单位(k)
function chkform(){
if(document.HwForm.Photo.value==""){
alert("请选择照片");
document.HwForm.Photo.focus();
return false;
}
if(document.HwForm.PhotoFileSize.value<=0){
alert("请重新选择照片");
document.HwForm.Photo.focus();
return false;
}
if(document.HwForm.PhotoFileSize.value>FileMaxSize*1024){
alert("选择的照片超过"+FileMaxSize+"K,请重新选择");
document.HwForm.Photo.focus();
return false;}
alert("可以提交,当前为测试");return false;
}
//-->
</SCRIPT>
<form method="POST" name="HwForm" onsubmit="return chkform();" enctype="multipart/form-data">


照片(请选择一个50K以内的图片文件):<input type="file" name="Photo" onpropertychange="if(document.HwForm.Photo.value!=''){document.getElementById('PhotoImg').src=document.HwForm.Photo.value;}"/>
<IMG style="POSITION: absolute; TOP: 100px;left:0px;" onerror="document.all.PhotoFileSize.value='-1';alert('请选择一个图像文件');" onload="if(document.getElementById('PhotoImg').fileSize<=1024*FileMaxSize){document.all.PhotoFileSize.value=document.getElementById('PhotoImg').fileSize;}else{alert('图片不能大于'+FileMaxSize+'K,请重新选择');document.all.PhotoFileSize.value='-1';}" style="display:none" id="PhotoImg">
<input size="3" type="hidden" name="PhotoFileSize" value="-1" />
<input type="submit" value="提交"> <input type="reset" value="重置">
<input type="button" onclick="alert(document.getElementById('PhotoImg').fileSize)"/>
</form>
</body>
</html>
[解决办法]
<system.web>
<httpRuntime maxRequestLength="40690"
useFullyQualifiedRedirectUrl="true"
executionTimeout="6000"
useFullyQualifiedRedirectUrl="false" 
minFreeThreads="8" 
minLocalRequestFreeThreads="4" 
appRequestQueueLimit="100" 
enableVersionHeader="true"
/>
</system.web>

http://www.cnblogs.com/wwyup/articles/1066522.html

热点排行