RT,别太复杂了,只有先判断下文件后缀,如果对的话就用save as 方法保存下图片就可以了,以前我的项目里有,但是找不到了,手写又比较麻烦,请高手共享下代码吧!

解决方案 »

  1.   

    string fn = fi.FileName;//取出的fi文件的名称        if (string.IsNullOrEmpty(fn) || fn.Length < 1)
            {
                
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('请上传图片!'); </script>");
            }
            else
            {
                string pSuffix = fn.Substring(fn.LastIndexOf(".") + 1);
                if (pSuffix.ToLower() != "jpg" && pSuffix.ToLower() != "bmp" && pSuffix.ToLower() != "png" && pSuffix.ToLower() != "gif")
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('请上传正确的图片类型!'); </script>");
                }
                else
                {
                    string pFileName = fn;//设置后的fi文件名
                    string pPath = Server.MapPath("../images/photo/") + pFileName;//上传到服务器的目录名称
                    fi.PostedFile.SaveAs(pPath);
                    string pVirtualPath = "images\\photo\\" + pFileName;                SqlParameter[] para ={ new SqlParameter("@id", SqlDbType.Int), new SqlParameter("@photoAddress", SqlDbType.NVarChar, 80), new SqlParameter("@photoExplain", SqlDbType.NVarChar, 80) };
                    para[0].Value = id;
                    para[1].Value = pVirtualPath;
                    para[2].Value = pExplain;
                    SqlHelper.executeNonQuery("update photo set photoAddress=@photoAddress,photoExplain=@photoExplain where id=@id", CommandType.Text, para);
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新成功!'); </script>");
                }
            }
      

  2.   

    参考
    http://www.cnblogs.com/weekzero/archive/2006/05/05/392003.html
      

  3.   


     protected void btnGoUp_Click(object sender, EventArgs e)
        {
            string str = this.File1.PostedFile.FileName;
            if (str == "")
            {
                Response.Write(bc.MassageBox("请选择上传图片!"));
                return;
            }        string ss = str.Substring(str.LastIndexOf("\\") + 1);
            string s = Server.MapPath("~\\photo\\" + ss);
            path = "~\\photo\\" + ss;
            if (File.Exists(s))
            {
                Response.Write(bc.MassageBox("该文件已经存在,请重新命名!!!"));
                return;
            }
            this.File1.PostedFile.SaveAs(s);
            Response.Write(bc.MassageBox("上传成功!"));
        }这个比较简单
      

  4.   

    http://download.csdn.net/source/872260  ASP.NET图片的上传
      

  5.   

    protected void BtnUpload_Click(object sender, EventArgs e)
        {
            bool fileOK = false;
            string path = Server.MapPath("~/Temp/");
            if (FileUpload1.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }
            if (fileOK)
            {
                try
                {
                    FileUpload1.SaveAs(path + FileUpload1.FileName);
                    LabMessage1.Text = "文件上传成功.";
                    LabMessage2.Text = "<b>原文件路径:</b>" + FileUpload1.PostedFile.FileName + "<br />" +
                                  "<b>文件大小:</b>" + FileUpload1.PostedFile.ContentLength + "字节<br />" +
                                  "<b>文件类型:</b>" + FileUpload1.PostedFile.ContentType + "<br />";
                }
                catch (Exception ex)
                {
                    LabMessage1.Text = "文件上传不成功.";
                }
            }
            else
            {
                LabMessage1.Text = "只能够上传图片文件.";
            }
        }