首页 技术 正文
技术 2022年11月19日
0 收藏 494 点赞 2,514 浏览 4979 个字

文件上传:
要使用控件 – 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(); }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919