Webform 文件上传、 C#加图片水印 、 图片验证码

文件上传:
要使用控件 - FileUpload
1、如何判断是否选中文件?
FileUpload.FileName -
选中文件的文件名,如果长度不大于0,那么说明没选中任何文件
js - f.value.length

2、如何保存到服务器上?
FileUpload.SaveAs("绝对路径");

3、如何获得绝对路径?
先编写相对路径 - "UpLoads/abc.txt"
将相对路径映射成绝对路径 - Server.MapPath("UpLoads/abc.txt");

4、现在只能上传成为txt文件,并且名字只能是abc
如何保留文件原有的名字和文件类型?
"UpLoads/" + FileUpload1.FileName;

5、如何防止重名覆盖的问题?
"UpLoads/" + Request.Cookies["user"].Value + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;

6、如何限制选中文件的类型?
限制普通人,给控件添加属性 - accept=".jpg,.png,.jpeg,.txt"
限制找事的:
document.getElementById("Button1").onclick = function () {
var fileName = document.getElementById("FileUpload1").value;

var name = fileName.substr(fileName.length - 4, 4);
var name1 = fileName.substr(fileName.length - 5, 5);
if (name != ".jpg" && name != ".png" && name != ".txt" && name1 != ".jpeg")
{
alert("请选择正确的文件!你要找事儿啊??");
return false;
}
};

7、控制上传文件的大小
扩容 - 系统默认允许最大上传长度是4MB
webconfig配置文件中写入
<system.web>
<compilation debug="false" targetFramework="4.0" />
<httpRuntime maxRequestLength="40000" />
</system.web>

百度搜,研究,C#大文件上传,断点续传。

注意!不要扩的太多,否则多人同时上传大文件会造成服务器内存不足!

限制大小
C#端限制:
if (FileUpload1.PostedFile.ContentLength>(4*1024*1024))
{
Label1.Text = "文件长度过长!!!";
return;
}

JS端限制:
var f = document.getElementById("FileUpload1");

if (f.files[0].size > (4 * 1024 * 1024)) {
alert("文件过大!!!");
return false;
}

上传图片加水印:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
#Image1 {
width: 500px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" />
<br />
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += Button1_Click;
} void Button1_Click(object sender, EventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent); Graphics g = Graphics.FromImage(img); string s = "汉企奇点网络0928";
Font f = new Font("微软雅黑",);
Brush b = new SolidBrush(Color.Red);
PointF p = new PointF(,); g.DrawString(s, f, b, p); string sss = "images/"+DateTime.Now.ToString("yyyyMMddhhmmssms")+FileUpload1.FileName; img.Save(Server.MapPath(sss)); Image1.ImageUrl = sss;
}
}

图片验证码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />
<asp:Button ID="Button1" runat="server" Text="验证" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
<script type="text/javascript">
var a = ;
document.getElementById("Image1").onclick = function () {
this.setAttribute("src", "yzm.aspx?id=" + a);
a++;
} </script>

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += Button1_Click;
} void Button1_Click(object sender, EventArgs e)
{
string t1 = TextBox1.Text;
string t2 = Session["YZM"].ToString(); if (t1.ToUpper() == t2.ToUpper())
{
Label1.Text = "验证成功!";
}
else
{
Label1.Text = "验证失败!";
} }
}

验证码图片:

(前台不用写)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing; public partial class YZM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap img = new Bitmap(, );
Graphics g = Graphics.FromImage(img);
Random r = new Random();
//单独加颜色
//List<Color> clist = new List<Color>();
//clist.Add(Color.Yellow);
//clist.Add(Color.Green);
//clist.Add(Color.Blue);
//clist.Add(Color.Pink);
//clist.Add(Color.Orange);
//clist.Add(Color.Black); //g.FillRectangle(new SolidBrush(clist[r.Next(0, clist.Count)]), 0, 0, 80, 40); //for (int i = 0; i < 10; i++)
//{
// Color ccc = clist[r.Next(0, clist.Count)]; // Pen ppp = new Pen(new SolidBrush(ccc), r.Next(1, 5)); // g.DrawLine(ppp, new PointF(r.Next(0, 80), r.Next(0, 40)), new PointF(r.Next(0, 80), r.Next(0, 40)));
//}
//随机数颜色rgba
Color yanse = Color.FromArgb(r.Next(), r.Next(), r.Next(), r.Next());
g.FillRectangle(new SolidBrush(yanse), , , , );
for (int i=;i<;i++)
{
Color yansel = Color.FromArgb(r.Next(), r.Next(), r.Next(), r.Next());
Pen ppp = new Pen(new SolidBrush(yansel), r.Next(, ));
g.DrawLine(ppp, new PointF(r.Next(, ), r.Next(, )), new PointF(r.Next(, ), r.Next(, )));
} string all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string s = ""; for (int i = ; i < ; i++)
{
int a = r.Next(all.Length);
s += all.Substring(a, );
} Session["YZM"] = s; g.DrawString(s, new Font("微软雅黑", ), new SolidBrush(Color.Red), new PointF(, )); img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
Response.End(); }
}
上一篇:Windows Server 2008安装 百度Wifi \ 360Wifi \ 小米Wifi 失败的解决方法


下一篇:jquery时间日期三级联动