这个不难,
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);
}
显示的部分自己加吧

解决方案 »

  1.   

    upFile.aspx
    <%@ 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= "上传失败!";
                }
            }
        }
    }
      

  2.   


    Regex reg = new Regex("(?<=[\\\\/]|^)[^\\\\/]*$", RegexOptions.IgnoreCase); 
    是多余的,可以不要
     
      

  3.   

    1,2 上页都说得较为轻楚了.
    3, 上传完后并能显示出来? 显示什么出来?
    *.rar    *.doc  *.jpg    *.bmp 
    这几种格式全然不同, 要显示呢. rar 还得解压,由于rar算法是专利,得使用它的exe
    而 doc,jpg,bmp 又是要显示什么呢.
      

  4.   

    如果是doc rar 就是显示链接啊
      

  5.   

    存放指定路径中,原后怎么按ID读出来??如果只把路径放入数据库,只接读数据库的路径就能读出来我是用gridviewproduct表:id user pic bool fileok = false;
            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;
                    }
            
            }
      

  6.   

    我也来个  自己写的比较简单
    但很受用 呵呵
    http://www.iyedu.cn/bbs/Show.Asp?ID=101
      

  7.   

    关键是用gridview怎么一一对应读出来我的思路是把路径写入数据库,然后用GIRIDVIEW直接显示出来