.net中,怎样设置文件上传的大小。
在Web.config中,我做了如下的设置,<httpRuntimemaxRequestLength="20971510"useFullyQualifiedRedirec...
在Web.config中,我做了如下的设置,<httpRuntime maxRequestLength="20971510" useFullyQualifiedRedirectUrl="true" executionTimeout="120"/>在上传文件的代码中,我写了如下的代码, if (FileUpload1.PostedFile.ContentLength < 2097151),怎么连2.5MB的文件都上传不了啊?
展开
3个回答
展开全部
实际例子(已上传图片为例)
protected void UpImg_Click(object sender, EventArgs e)//上传图片
{
if (FileUpload1.PostedFile.ContentLength < 500000)//100000为100K
{
string fileFullname = this.FileUpload1.FileName;
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
{
this.FileUpload1.SaveAs(Server.MapPath("../upload") + "\\" + dataName + "." + type);
string ProImg = "upload/" + dataName + "." + type;
this.txtProImg.Text = "upload/" + dataName + "." + type;
}
else
{
Response.Write("<script language='javascript'>alert('支持格式:|jpg|gif|bmp|');</script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('你的图片已经超过500K的大小!');</script>");
}
}
web.config配置
web.config文件的内容大体如此:
<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
<sessionState mode="InProc" cookieless="false" timeout="20"/>
<httpRuntime maxRequestLength="47185920">
</httpRuntime>
</system.web>
</configuration>
注意在文件中的位置,
讲解:
<sessionState mode="InProc" cookieless="false" timeout="20"/>
//cookieless="false",不使用cookies
//timeout="20",会话时间为20分钟,单位是分钟,这里可自行修改
<httpRuntime maxRequestLength="47185920">
//站点默认上传的最大文件
protected void UpImg_Click(object sender, EventArgs e)//上传图片
{
if (FileUpload1.PostedFile.ContentLength < 500000)//100000为100K
{
string fileFullname = this.FileUpload1.FileName;
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
{
this.FileUpload1.SaveAs(Server.MapPath("../upload") + "\\" + dataName + "." + type);
string ProImg = "upload/" + dataName + "." + type;
this.txtProImg.Text = "upload/" + dataName + "." + type;
}
else
{
Response.Write("<script language='javascript'>alert('支持格式:|jpg|gif|bmp|');</script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('你的图片已经超过500K的大小!');</script>");
}
}
web.config配置
web.config文件的内容大体如此:
<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
<sessionState mode="InProc" cookieless="false" timeout="20"/>
<httpRuntime maxRequestLength="47185920">
</httpRuntime>
</system.web>
</configuration>
注意在文件中的位置,
讲解:
<sessionState mode="InProc" cookieless="false" timeout="20"/>
//cookieless="false",不使用cookies
//timeout="20",会话时间为20分钟,单位是分钟,这里可自行修改
<httpRuntime maxRequestLength="47185920">
//站点默认上传的最大文件
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
FileUpload1.PostedFile.ContentLength < 2097151 小于2M?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
修改machine.config试试
追问
不懂。
追答
maxRequestLength="20971510" 怎么设置这么大
maxRequestLength="40960" 40M
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询