包括增删上传控件,保存文件,保存文件名到数据库。
求源码

解决方案 »

  1.   


      protected void Page_Load(object sender, EventArgs e)
        {
            getsize(Server.MapPath("/web/aaa/"));    }    #region 图片上传
        protected void Button1_Click(object sender, EventArgs e)
        {        //判断上传控件中是否有值
            if (FileUpimage.HasFile == false)
            {
                Response.Write(" <script lanage='javascript'> alert('请指定上传的图片') </script>");
            }
            else
            {
                string fType = FileUpimage.PostedFile.ContentType;//获取图像的类型
                if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png")
                {
                    //获取文件信息
                    FileInfo file = new FileInfo(FileUpimage.PostedFile.FileName);                System.DateTime currentTime = new System.DateTime();
                    currentTime = DateTime.Now;
                    string stamp = currentTime.Year.ToString() + currentTime.Month.ToString() + currentTime.Day.ToString() + currentTime.Hour.ToString() + currentTime.Minute.ToString() + currentTime.Second.ToString() + currentTime.Millisecond.ToString();                //原始图片保存路径
                    string path = "/web/picure/usr/" + stamp + ".gif";
                    //缩略图保存路径
                    string spath = "/web/picure/usr/sPic/Thumb_" + stamp + ".gif";
                    try
                    {
                        //原始图片保存
                        FileUpimage.SaveAs(Server.MapPath(path));
                        //缩略图保存
                        MakeThumbImage(Server.MapPath(path), Server.MapPath(spath), 160, 120);
                        //给隐藏的图片控件赋值并显示
                        Image1.Visible = true;
                        Image1.ImageUrl = spath;
                        Response.Write(" <script lanage='javascript'> alert('图片上传成功') </script>");
                    }
                    catch
                    {
                        Response.Write(" <script lanage='javascript'> alert('图片上传失败') </script>");
                    }
                }
                else
                {
                    Response.Write(" <script lanage='javascript'> alert('上传图片格式不正确!请上传jpg/jpge/gif/bmp/png格式的图片!') </script>");
                }
            }
        } 
        #endregion    #region 图片压缩
        /// <summary>
        ///创建日期:2009-6-08
        ///创建人  :周昕
        ///方法名称:MakeThumbImage
        ///内容摘要:生成缩略图
        /// </summary>
        /// <param name="sPath">源图路径(物理路径) </param>
        /// <param name="stPath">缩略图路径(物理路径) </param>
        /// <param name="nWidth">缩略图宽度 </param>
        /// <param name="nHeight">缩略图高度 </param>
        private void MakeThumbImage(string sPath, string stPath, int nWidth, int nHeight)
        {
            System.Drawing.Image sImage = System.Drawing.Image.FromFile(sPath);
            int tw = nWidth;
            int th = nHeight;
            ///原始图片的宽度和高度
            int sw = sImage.Width;
            int sh = sImage.Height;
            if (sw > tw)
            {
                sw = tw;
            }
            if (sh > th)
            {
                sh = th;
            }
            System.Drawing.Bitmap objPic, objNewPic;
            objPic = new System.Drawing.Bitmap(sPath);
            objNewPic = new System.Drawing.Bitmap(objPic, sw, sh);
            objNewPic.Save(stPath);
            sImage.Dispose();
            objPic.Dispose();
            objNewPic.Dispose();
        } 
        #endregion    #region 创建文件夹
        protected void Button2_Click(object sender, EventArgs e)
        {        /*C创建文件夹并设置权限*/
            /*    
    需要添加以下命名空间:    
    using System.IO;    
    using System.Security.AccessControl;    
    */        string Path = Server.MapPath("/web/picure/usr/");
            string sPath = Server.MapPath("/web/picure/usr/sPic");
            Directory.CreateDirectory(Path);
            addpathPower(Path, "ASPNET", "FullControl");
            Directory.CreateDirectory(sPath);
            addpathPower(sPath, "ASPNET", "FullControl");
        }
        /*///////C创建文件夹并设置权限////////////////*/    public void addpathPower(string pathname, string username, string power)
        {        DirectoryInfo dirinfo = new DirectoryInfo(pathname);        if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
            {
                dirinfo.Attributes = FileAttributes.Normal;
            }        //C创建文件夹取得访问控制列表     
            DirectorySecurity dirsecurity = dirinfo.GetAccessControl();        switch (power)
            {
                case "FullControl":
                    dirsecurity.AddAccessRule(new FileSystemAccessRule(
                    username, FileSystemRights.FullControl,
                    InheritanceFlags.ContainerInherit,
                    PropagationFlags.InheritOnly, AccessControlType.Allow));
                    break;
                case "ReadOnly":
                    dirsecurity.AddAccessRule(
                 new FileSystemAccessRule(username,
                 FileSystemRights.Read, AccessControlType.Allow));
                    break;
                case "Write":
                    dirsecurity.AddAccessRule(
                    new FileSystemAccessRule(username,
                    FileSystemRights.Write, AccessControlType.Allow));
                    break;
                default:
                    break;        }
        } 
        #endregion    #region 删除文件夹    protected void Button3_Click(object sender, EventArgs e)
        {
            string sPath = Server.MapPath("/web/picure/u/");
            try
            {            CleanFiles(sPath);
                CleanFiles(sPath);
                Response.Write(" <script lanage='javascript'> alert('成功') </script>");
            }
            catch
            {
                Response.Write(" <script lanage='javascript'> alert('失败') </script>");
            }    }    private static void CleanFiles(string sPath)
        {
            try
            {
                string[] dirs = Directory.GetDirectories(sPath);            string[] files = Directory.GetFiles(sPath);
                if (0 != dirs.Length)
                {                foreach (string subDir in dirs)
                    {                    if (null == Directory.GetFiles(subDir))
                        {
                            Directory.Delete(subDir, true);                        return;                    }                    else { CleanFiles(subDir); }                }            }
                if (0 != files.Length)
                {                foreach (string file in files)
                    {                    File.Delete(file);
                    }            }
                else
                {
                    Directory.Delete(sPath);
                }
            }
            catch { }    } 
        #endregion
        private void getsize(string sPath)
        {        string[] files = Directory.GetFiles(sPath);
            
            long str = 0;
            for(int i=0;i<files.Length;i++)
            {
                FileInfo file = new FileInfo(files[i]);
                str += file.Length;
            }
           long a = str / (1024*1024);
            Response.Write(a);
        }
    这是图片的,文件和他差不多,你自己研究吧
      

  2.   

    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
            function addFileUpLoad(){
                var str = '<INPUT type="file" size="43" NAME="File">';
                document.getElementById('addFileupLoad').insertAdjacentHTML("beforeEnd",str);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="addFileupLoad" style="width: 436px;">
        <asp:Button ID="Button1" runat="server" 
                Style="position: relative;left:140px; top: -1px;" Text="上 传" 
                Width="61px" onclick="Button1_Click"/>
                    <asp:Label ID="Label1" runat="server" Font-Size="Smaller" 
                ForeColor="Green" Style="
                        position: relative;cursor:pointer;left:150px; top: 6px; height: 34px; width: 68px;" 
                Text="增加文件"></asp:Label>
        <asp:FileUpload ID="FileUpload2" runat="server" Width="363px" /><br />
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="363px" /><br />
        </div>
        </form>
    </body>
    protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Attributes.Add("onclick", "addFileUpLoad()");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            HttpPostedFile postedFile = null;
            string fileName = null;
            String path = Server.MapPath("~/UpdaterTest/");
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                postedFile = files[iFile];
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                postedFile.SaveAs(path + fileName);
            }
        }
      

  3.   

    增删上传控件
    -----
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
            function addFileUpLoad(){
                var str = '<INPUT type="file" size="43" NAME="File">';
                document.getElementById('addFileupLoad').insertAdjacentHTML("beforeEnd",str);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="addFileupLoad" style="width: 436px;">
        <asp:Button ID="Button1" runat="server" 
                Style="position: relative;left:140px; top: -1px;" Text="上 传" 
                Width="61px" onclick="Button1_Click"/>
                    <asp:Label ID="Label1" runat="server" Font-Size="Smaller" 
                ForeColor="Green" Style="
                        position: relative;cursor:pointer;left:150px; top: 6px; height: 34px; width: 68px;" 
                Text="增加文件"></asp:Label>
        <asp:FileUpload ID="FileUpload2" runat="server" Width="363px" /><br />
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="363px" /><br />
        </div>
        </form>
    </body>
    保存文件,lz自己加入插入数据库代码..
    ------
    protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Attributes.Add("onclick", "addFileUpLoad()");
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            HttpPostedFile postedFile = null;
            string fileName = null;
            String path = Server.MapPath("~/UpdaterTest/");
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                postedFile = files[iFile];
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                postedFile.SaveAs(path + fileName);
            }
        }
      

  4.   

    http://topic.csdn.net/u/20091123/09/1983e0e3-6019-4ba3-b39c-73d514fc4196.html