fileUpload上传图片怎么提示文件过大?

bianjijianjie 2010-01-23 11:26:15
如果上传的图片超过200k就提示文件要在200k以下
怎么做啊?
我的上传代码如下

protected void Button1_Click(object sender, EventArgs e)
{
Boolean fileOk = false;
if (FileUp.HasFile)
{
string savePath = Server.MapPath("~/Upload/");
string sExt = FileUp.FileName.Substring(FileUp.FileName.LastIndexOf(".")).ToLower();
string filename = DateTime.Now.ToString("yyyymmddhhmmssfff") + sExt;
savePath += filename;
string[] allowExtension = { ".jpg", ".gif",".png" };
for (int i = 0; i < allowExtension.Length; i++) //对上传的文件的类型进行一个个匹对
{
if (sExt == allowExtension[i])
{
fileOk = true;
break;
}
}
if (fileOk == true)
{
FileUp.SaveAs(savePath); //保存文件
}
else
{
Response.Write("<script>alert('图片类型不正确!')</script>");
}
}
else
{
Response.Write("<script>alert('请选择图片!')</script>");
}
}
...全文
475 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
a1470 2010-01-25
  • 打赏
  • 举报
回复
写图片的时候判断下大小不就完了
houdilzc 2010-01-25
  • 打赏
  • 举报
回复
if (uploadFile.PostedFile.ContentLength > 204800)
{
//图片必须小于200KB
return;
}
stonewjx 2010-01-24
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string savePath = Server.MapPath("~/Upload/");
string filePath = FileUpload1.PostedFile.FileName;
string fileExt = Path.GetExtension(filePath);
IList allowExt = new string[] { ".txt", ".doc",".jpg",".gif" };
if (allowExt.Contains(fileExt))
{
if (FileUpload1.PostedFile.ContentLength<2000)
{
savePath += DateTime.Now.ToString("HHmmss") + fileExt;
FileUpload1.SaveAs(savePath);//成功
}
}
}
}
lovelium 2010-01-24
  • 打赏
  • 举报
回复
楼主就用FileUpload1 .PostedFile .ContentLength>204800这个就可以了。
xray2005 2010-01-24
  • 打赏
  • 举报
回复
文件大小不能超过200K应该是:FileUpload1 .PostedFile .ContentLength>204800
gdlpc 2010-01-24
  • 打赏
  • 举报
回复
(FileUpload1.PostedFile.ContentLength / 1024) + "KB"
gdlpc 2010-01-24
  • 打赏
  • 举报
回复
FileUpload1.PostedFile.ContentLength / 1024) + "KB"
stonewjx 2010-01-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 junloveyan 的回复:]
在web.config中添加httpRuntime元素,如下:
   <configuration>
    <system.web>
        <httpRuntime maxRequestLength="8192"
          useFullyQualifiedRedirectUrl="true"
          executionTimeout="45"
          versionHeader="1.1.4128"/>
    </system.web>
   </configuration>
  其中maxRequestLength属性就是限制上传大小的,如设为"8192"即为8M。

[/Quote]
好用
jack15850798154 2010-01-23
  • 打赏
  • 举报
回复
前台页面upload.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="_upload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>上传文件</title>
</head>
<body >
<form id="form" runat="server" method="post">
<input type="file" id="UpImage" runat="server" name="UpImage"/>
<asp:Button ID="upload" runat="server" OnClick="upload_Click" Text="上传" />

</body>
</html>
后台upload.aspx.cs代码
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _upload : System.Web.UI.Page
{ string name;//根据需要给上传的图片命名
int minsize;//上传文件的最低大小,单位kb
int maxsize;;//上传文件的最大大小,单位kb

protected void upload_Click(object sender,EventArgs e)
{
string fileName, fileExtension, FullName;
if (UpImage.PostedFile != null)
{
if (UpImage.PostedFile.ContentLength != 0 && UpImage.PostedFile.ContentLength <= (1024 * maxsize) && UpImage.PostedFile.ContentLength >= (1024 * minsize))
{
fileName = System.IO.Path.GetFileName(UpImage.PostedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName);
if (IsExtension(fileExtension))
{
try
{
FullName = name;
CreateDir(folder);
FullName = DateTime.Now.ToString("yyyyMM") + "/" + FullName;
UpImage.PostedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(@"UpFiles/" + folder + "/") + FullName);
}
catch (Exception ex)
{
Response.Write("<script language=javascript>alert('" + ex.Message + "');</script>");
}
}
else
{
Response.Write("<script language=javascript>alert('上传的文件格式不对,请上传指定的文件格式!');</script>");
}
}
else
{
Response.Write("<script language=javascript>alert('没有上传文件,或者文件太大!请重新上传!');</script>");
}
}

}

private void CreateDir(string folder)
{

string dirByMonth = "", dirByDay = "";

dirByMonth = DateTime.Now.ToString("yyyyMM");

dirByDay = DateTime.Now.ToString("dd");

string dir = System.Web.HttpContext.Current.Request.MapPath(@"UpFiles/" + folder + "/" + dirByMonth);//+ "/" + dirByDay);

try
{

if (!Directory.Exists(dir))
{

Directory.CreateDirectory(dir);

}

}

catch (Exception e)
{

Response.Write("<script language=javascript>alert('" + e.Message + "');</script>");

}

finally { }

}

//这个函数是创建文件名

private string CreateFileName()
{

string strR = "";

strR += DateTime.Now.ToString("HH");

strR += DateTime.Now.ToString("mm");

strR += DateTime.Now.ToString("sss");

Random roo = new Random();

strR += roo.Next(11111, 99999);

return strR;

}


private bool IsExtension(string Extension)
{
if (Extension == ".jpg")
return true;
else
return false;
}

}

bianjijianjie 2010-01-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 imsasuke 的回复:]
引用 1 楼 arjsyy 的回复:
FileUpload1 .PostedFile .ContentLength这个是获取文件的大小

FileUpload1 .PostedFile .ContentLength>200这样写吗
不行啊,我只有 40K的也提示超过200k了
怎么搞的啊
[/Quote].........
没人解答啊?
bianjijianjie 2010-01-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 arjsyy 的回复:]
FileUpload1 .PostedFile .ContentLength这个是获取文件的大小
[/Quote]
FileUpload1 .PostedFile .ContentLength>200这样写吗
不行啊,我只有 40K的也提示超过200k了
怎么搞的啊
cloudgamer 2010-01-23
  • 打赏
  • 举报
回复
ff可以但ie的js判断不了文件大小
可以参考这个无刷新上传系统
利用返回的数据来判断
xmlxslt 2010-01-23
  • 打赏
  • 举报
回复
这里面有个上传头像的例子,你看看
http://d.download.csdn.net/down/1959774/xmlxslt
arjsyy 2010-01-23
  • 打赏
  • 举报
回复
FileUpload1 .PostedFile .ContentLength这个是获取文件的大小
yuanlovechina 2010-01-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 imsasuke 的回复:]
引用 4 楼 imsasuke 的回复:
引用 1 楼 arjsyy 的回复:
FileUpload1 .PostedFile .ContentLength这个是获取文件的大小

FileUpload1 .PostedFile .ContentLength>200这样写吗
不行啊,我只有 40K的也提示超过200k了
怎么搞的啊
.........
没人解答啊?
[/Quote]
文件大小不能超过200K应该是:FileUpload1 .PostedFile .ContentLength>204800
junloveyan 2010-01-23
  • 打赏
  • 举报
回复
在web.config中添加httpRuntime元素,如下:
  <configuration>
   <system.web>
   <httpRuntime maxRequestLength="8192"
   useFullyQualifiedRedirectUrl="true"
   executionTimeout="45"
   versionHeader="1.1.4128"/>
   </system.web>
  </configuration>
  其中maxRequestLength属性就是限制上传大小的,如设为"8192"即为8M。
rememberhai 2010-01-23
  • 打赏
  • 举报
回复
记得要转换单位。
rememberhai 2010-01-23
  • 打赏
  • 举报
回复
用File1.PostedFile.ContentLength获取文件大小。加个判断就O了。
wuyq11 2010-01-23
  • 打赏
  • 举报
回复
int fsize = File1.PostedFile.ContentLength;
if (fsize > 1024 * 500)
{
ClientScript.RegisterStartupScript(this.GetType(), "message", "<script language='javascript' defer>alert('');</script>");
return;
}
目的和意义  小区管理是现代温馨小区必不可少的一部分,互联网的高速发展,势必将传统的小区管理模式转变为“互联网+”的小区管理模式,人们已经融入到了互联网的时代中。     本系统的目的是为了方便管理小区,为了方便居民交流。     本系统的意义有,第一,本系统能方便的进行物业管理,从而减轻小区物业管理员的负担;第二,本系统有交流管理模块,能让居民在小区里多些交流,能发布一些有趣的活动通知,提高居民的幸福感。新型小区管理模式的小趣社核心是互联网+小区物业管理+通知交流。项目介绍  这是一个基于ssm的互联网+小区物业管理+通知交流的项目  技术点:    使用Mybatis分页插件PageHelper    使用redis做Mybatis的二级缓存    使用Spring自带的md5加密工具类    使用commons-fileupload文件上传组件上传文件    使用Spring的拦截器控制访问    使用阿里巴巴的druid连接池    maven项目    使用ssm框架    后台bootstrap前端框架    轮播图    富文本编辑框                echarts报表数据展示        POIexcel文件导入导出               微信扫码支付                邮件和短信发送功能模块前台功能后台功能项目部分截图前台截图登录界面  主界面 主界面 发动态界面 修改信息界面    数据统计页面         

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧