您的位置:首页 > 其它

Web.config中配置上传文件大小与判断上传文件的类型等一系列规则的方法

2012-11-16 15:08 591 查看
web.config中:

<!-- 设置文件上传时的系统参数-->

<httpRuntime maxRequestLength="10240"

useFullyQualifiedRedirectUrl="true"

executionTimeout="6000"

minFreeThreads="8"

minLocalRequestFreeThreads="4"

appRequestQueueLimit="100"

/>

返回错误信息的方法:

此方法为初始调用的方法为了判断错误返回错误信息

//执行上传操作

/**//// <summary>

/// 执行上传操作

/// getFileName 需要指定来自方法:getUpFileName返回的文件名

/// FileType "Image" 或 "Media" 或 "Flash"

/// ThisMatch 指定你当前程序所在的目录,哪是不断空一定要在最后加"/",例如"../"

/// a.asf -> 200609090909.asf

/// </summary>

public string UpfilesInstance(System.Web.UI.HtmlControls.HtmlInputFile UpFileControl,string getFileName,string FileType,string ThisMatch)

{

string upfilepath = string.Empty ;

string Errorstr = string.Empty ;

getFileName = getFileName.Substring(getFileName.LastIndexOf("\\")+1);

switch(CheckFnameEx(getFileName,FileType,UpFileControl.PostedFile.ContentLength/1024 ))

{

case "1":

switch(FileType)

{

case "Media":

Errorstr = "上传的文件格式不正确,只能是WMV、WMA、MP3";

break;

case "Image":

Errorstr ="上传的文件格式不正确,只能是JPG、GIF、JPEG";

break;

case "Flash":

Errorstr ="上传的文件格式不正确,只能是WMV、SWF";

break;

}

break;

case "2":

switch(FileType)

{

case "Image":

Errorstr = "上传的文件不成功";

break;

case "Media":

Errorstr ="上传的文件太大不能超过10M";

break;

case "Flash":

Errorstr ="上传的文件不成功";

break;

}

break;

case "3":

Errorstr ="上传的文件不成功";

break;

case "0":

Errorstr = null;

break;

}

//上传文件

if(Errorstr==null)

{

try

{

switch(FileType)

{

case "Image":

upfilepath = ThisMatch +System.Configuration.ConfigurationSettings.AppSettings["upPhotoPath"].ToString();

break;

case "Media":

upfilepath = ThisMatch +System.Configuration.ConfigurationSettings.AppSettings["upPrdctionPath"].ToString();

break;

case "Flash":

upfilepath = ThisMatch +System.Configuration.ConfigurationSettings.AppSettings["upPhotoPath"].ToString();

break;

}

UpFileControl.PostedFile.SaveAs(Server.MapPath(upfilepath) + getFileName);

Errorstr = "1";//OK

}

catch

{

Errorstr ="上传的文件出错";

}

}

return Errorstr;

}

验证上传的文件:

<add key="maxRequestLength" value="10240" /> 10M

filelength上传的文件长度(是字节)应该 /1024 在与配置文件设置值比较.

验证上传的文件

/**//// <summary>

/// 注册用户登录验证,fname为校验文件名,filetype为"Image" 或者 "Media"

/// return 0-验证通过,1-文件格式不对,2-文件过大,3-未知错误

/// </summary>

public string CheckFnameEx(string fname,string filetype,int filelength)

{

if(fname=="" && filetype=="")

{

return "3";

}

//得到文件的后缀名

string exName=fname.Substring(fname.LastIndexOf(".")+1).ToUpper();

if(exName =="")

{

return "3";

}

string checkfilelen =string.Empty ;

switch(filetype)

{

case "Image":

if(exName!="JPG" && exName!="GIF" && exName!="JPEG")

{

return "1";

}

break;

case "Media":

checkfilelen = System.Configuration.ConfigurationSettings.AppSettings["maxRequestLength"].ToString();

if(exName!="WMA" && exName!="WMV" && exName!="MP3")

{

return "1";

}

else

{

if(filelength > int.Parse(checkfilelen))//判断文件是否大于10M

{

return "2";

}

}

break;

case "Flash":

if(exName!="SWF" && exName!="WMV")

{

return "1";

}

break;

}

return "0";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: