这个不难,
private bool CheckPic()
{
bool isSafe = false;
HttpPostedFile pic = picfile.PostedFile;
//扩展名检查
string picext = System.IO.Path.GetExtension(picfile.PostedFile.FileName).ToLower();
if(picext == ".jpg" || picext==".gif" || picext == "bmp" || picext =="png")
{
isSafe = true;
}
else
{
isSafe = false;
Kit.Alert(this.Page,"图片格式不对,请转换成常见的图片格式 jpg,gif,bmp,png");
return isSafe;
}
//图片大小检查
if(pic.ContentLength > PicMaxLength())
{
isSafe = false;
Kit.Alert(this.Page,"超过图片限制大小");
return isSafe;
}
//图片尺寸检查
System.IO.Stream picstream = pic.InputStream;
System.Drawing.Image img = System.Drawing.Image.FromStream(picstream);
if(img.Width > 0 && img.Height >0)
{
isSafe = true;
//第三步验证身份证图片并上传
PicFileName = GetPicPath(picext);
PicSavePath = PicPath() + PicFileName;
SaveIdcard(PicSavePath); picstream.Close();
picstream.Flush();
}
else
{
isSafe = false;
Kit.Alert(this.Page,"非法的图片文件");
//picstream.Close();
//picstream.Flush();
return isSafe;
}
return isSafe;
}
private string GetPicPath(string ext)
{
return DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+System.Guid.NewGuid().ToString().Substring(1,4)+ ext;
}
private void SaveIdcard(string idcard)
{
//string savepath = PicPath();
//savepath += idcard+".jpg";
picfile.PostedFile.SaveAs(idcard);
}
显示的部分自己加吧
private bool CheckPic()
{
bool isSafe = false;
HttpPostedFile pic = picfile.PostedFile;
//扩展名检查
string picext = System.IO.Path.GetExtension(picfile.PostedFile.FileName).ToLower();
if(picext == ".jpg" || picext==".gif" || picext == "bmp" || picext =="png")
{
isSafe = true;
}
else
{
isSafe = false;
Kit.Alert(this.Page,"图片格式不对,请转换成常见的图片格式 jpg,gif,bmp,png");
return isSafe;
}
//图片大小检查
if(pic.ContentLength > PicMaxLength())
{
isSafe = false;
Kit.Alert(this.Page,"超过图片限制大小");
return isSafe;
}
//图片尺寸检查
System.IO.Stream picstream = pic.InputStream;
System.Drawing.Image img = System.Drawing.Image.FromStream(picstream);
if(img.Width > 0 && img.Height >0)
{
isSafe = true;
//第三步验证身份证图片并上传
PicFileName = GetPicPath(picext);
PicSavePath = PicPath() + PicFileName;
SaveIdcard(PicSavePath); picstream.Close();
picstream.Flush();
}
else
{
isSafe = false;
Kit.Alert(this.Page,"非法的图片文件");
//picstream.Close();
//picstream.Flush();
return isSafe;
}
return isSafe;
}
private string GetPicPath(string ext)
{
return DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+System.Guid.NewGuid().ToString().Substring(1,4)+ ext;
}
private void SaveIdcard(string idcard)
{
//string savepath = PicPath();
//savepath += idcard+".jpg";
picfile.PostedFile.SaveAs(idcard);
}
显示的部分自己加吧
解决方案 »
- Datalist分页???
- 怎样把网页中的图片,表格,链接等导入到WORD(格式不乱)
- dl样式的问题
- 我刚用了VS2005做了一个网站,在调试状态下一切正常,可在IIS里浏览的时候就老报web.config错误
- [问题]在ASP.NET中动态添加TableRow中出现问题...
- http://www.interone.de/ 探讨这个网站的技术,菜鸟勿进|||||||
- 请问一下一个比较水的问题?是不是.net编程是不是必须用 ms.visual系列呀。
- 大家说这个如何实现?
- 在线等!!!关于页面登陆问题---菜鸟---很急哦
- 简单问题,在线等待,快速给分!
- gridview根据条件加色彩
- 求研究所资料管理系统
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upFile.aspx.cs" Inherits="Web.admin.imgLinks.upFile" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="Label"></asp:Label>
<br />
<asp:FileUpload ID="fileImg" runat="server" Width="240px" />
<asp:Button ID="btSubmit" runat="server" Text="确定" CssClass="addBtn" OnClick="btSubmit_Click"
ValidationGroup="submit" />
</form>
</body>
</html>
upFile.aspx.cs
using System;
using System.Text.RegularExpressions;namespace Web.admin.imgLinks
{
public partial class upFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
} protected void btSubmit_Click(object sender, EventArgs e)
{
if (!fileImg.HasFile)
{
Label1.Text = "请选择上传图片/Flash!";
return;
} string ftStr = "|.rar|.doc|.jpg|.bmp|";
if (ftStr.IndexOf("|" + System.IO.Path.GetExtension(fileImg.FileName).ToLower() + "|") < 0)
{
Label1.Text = "文件文件格式错误!";
return;
} if (fileImg.PostedFile.ContentLength > 1024 * 1024)
{
Label1.Text = "文件超过1MB!";
return;
}
Regex reg = new Regex("(?<=[\\\\/]|^)[^\\\\/]*$", RegexOptions.IgnoreCase);
string saveFile = Server.MapPath(".") + "/" + System.IO.Path.GetFileName(fileImg.FileName);
fileImg.SaveAs(saveFile); if (System.IO.File.Exists(saveFile))
{
Label1.Text = String.Format("上传成功! <a target='_blank' href='./{0}'>{0}</a>", System.IO.Path.GetFileName(fileImg.FileName));
}
else
{
Label1.Text= "上传失败!";
}
}
}
}
Regex reg = new Regex("(?<=[\\\\/]|^)[^\\\\/]*$", RegexOptions.IgnoreCase);
是多余的,可以不要
3, 上传完后并能显示出来? 显示什么出来?
*.rar *.doc *.jpg *.bmp
这几种格式全然不同, 要显示呢. rar 还得解压,由于rar算法是专利,得使用它的exe
而 doc,jpg,bmp 又是要显示什么呢.
string path = Server.MapPath("~/temp/");
if(this.FileUpload1.HasFile)
{
string fileexception = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
string[] allowedexception = { ".gif", ".bmp", ".png", ".jpg", ".rar" };
for(int i=0;i<allowedexception.Length;i++)
{
if(fileexception==allowedexception[i])
fileok=true;
}
}
但很受用 呵呵
http://www.iyedu.cn/bbs/Show.Asp?ID=101