我是刚学这个的,现在在做一个网站要用到这个功能,希望好人能给我一个这样的源码,最好能有注释,这样我就更容易看得懂了,我要的功能很简单:
1、可以限制图片的大小。
2、能指定上传的格式。
3、能实现缩略图。
大概就这些,谢谢好人了。

解决方案 »

  1.   

    string fullname = FileUpload1.FileName.ToString();//直接取得文件名
                string url = FileUpload1.PostedFile.FileName.ToString();//取得上传文件路径
                string typ = FileUpload1.PostedFile.ContentType.ToString();//获取文件MIME内容类型
                string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型
                int size = FileUpload1.PostedFile.ContentLength;//获取文件格式的大小            //下面是保存,判断
                if (File.Exists(url))
                {
                    Response.Write("<script>alert('文件已存在 !')</script>");
                }
                else
                {
                    if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png" || typ2 == "rar")
                     {
                        if (size <= 4134904)
                        {
                            FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + fullname);//Server.MapPath("uploads\\image") + "\\" +                         //fullname将文件保存在跟目录的uploads文件夹下
                            Response.Write("<script>alert('上传成功');</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('你的文件超过限制大小!')</script>");
                        }                }
                    else
                    {
                        Response.Write("<script>alert('上传文件格式不正确)</script>");                }            }
      

  2.   

    string fileName = this.FileUpload_Import.PostedFile.FileName;
                string _fileExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
                if (_fileExt.ToLower() != "txt")
                {
                    ScriptHelper.AlertAndGoHistory("暂不允许导入除txt文件外的其他格式文件!", -1);
                    Response.End();
                }
                if (this.FileUpload_Import.PostedFile.ContentLength > 2097152)
                {
                    ScriptHelper.AlertAndGoHistory("导入的文件大小不能超过2MB!", -1);
                    Response.End();
                }
                try
                {
                    //上传读取导入的txt文件
                    string filePath = Server.MapPath("/Upload/") + this.FileUpload_Import.FileName;
                    this.FileUpload_Import.PostedFile.SaveAs(filePath);
                    string[] values = File.ReadAllLines(filePath);
                    //删除上传文件
                    System.IO.FileInfo file = new System.IO.FileInfo(filePath);
                    if (file.Exists)
                    {
                        file.Delete();
                    }
                    //将数据导入数据库
                    int success = 0;
                    if (SpreadCustomerInfo.BatchInsertInfo(values, out success))
                    {
                        ScriptHelper.AlertAndRedirect("导入完成,成功导入" + success.ToString() + "条数据", "/Manager/SMSSpreadUserInfo.aspx");
                    }
                    else
                    {
                        ScriptHelper.AlertAndRedirect("导入失败!", "/Manager/SMSSpreadUserInfo.aspx");
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error("导入数据失败:{0}", ex.ToString());
                    ScriptHelper.AlertAndRedirect("导入数据失败,导入文件错误!", "/Manager/SMSSpreadUserInfo.aspx");
                }
      

  3.   

     asp.net上传图片并同时生成缩略图