×¢²á µÇ¼
±à³ÌÂÛ̳ ¡ú ASP.NET¼¼ÊõÂÛ̳

ΪʲôִֻÐÐelseÓï¾ä£¿

Teem ·¢²¼ÓÚ 2013-07-30 17:52£¬ 746 ´Îµã»÷
³ÌÐò´úÂ룺
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class FileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bool filesValib = false;
        if (this.FileUpload1.HasFile)
        {
            string fileExtension = (this.FileUpload1.FileName).ToLower();
            string[] restricExtension = { "gif", "jpg", "bmp", "png" };
            for (int i = 0; i < restricExtension.Length; i++)
            {
                if (fileExtension == restricExtension[i])
                {
                    filesValib = true;
                }
            }

            if (filesValib == true)
            {
                try
                {
                    this.Image1.ImageUrl = "../../WebSite7/Image/password.gif";
                    this.FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);
                    this.Label1.Text = "ÎļþÉÏ´«³É¹¦";
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "Ô´Îļþ·¾¶£º" + this.FileUpload1.PostedFile.FileName;
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "Îļþ´óС£º" + this.FileUpload1.PostedFile.ContentLength + "×Ö½Ú";
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "ÎļþÀàÐÍ:" + this.FileUpload1.PostedFile.ContentType;
                }

                catch
                {
                    this.Label1.Text = "ÎļþÉÏ´«²»³É¹¦£¡";
                }

                finally
                {

                }
            }
            else

            {
                this.Label1.Text = "Ö»Äܹ»ÉÏ´«ºó׺ÃûΪjpg, gif, png, bmpµÄÎļþ";
            }
        }
    }
}

Ñ¡ÖÐͼƬ¸ñʽµÄµÄÎļþµã»÷ÉÏ´«  »¹ÊÇÖ±½ÓÌøµ½elseÓï¾ä  Çó½âÊÍ
7 »Ø¸´
#2
shangsharon2013-07-30 23:50
string[] restricExtension = { "gif", "jpg", "bmp", "png" };
¸ÄΪ
string[] restricExtension = { ".gif", ".jpg", ".bmp", ".png" };
#3
Teem2013-07-31 09:15
¸Ðл
#4
Teem2013-07-31 09:29
³ÌÐò´úÂ룺
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class FileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bool filesValib = false;
        if (this.FileUpload1.HasFile)
        {
            string fileExtension = (this.FileUpload1.FileName).ToLower();
            string[] restricExtension = { ".gif", ".jpg", ".bmp", ".png" };
            for (int i = 0; i < restricExtension.Length; i++)
            {
                if (fileExtension == restricExtension[i])
                {
                    filesValib = true;
                }
            }

            if (filesValib == true)
            {
                try
                {
                    this.Image1.ImageUrl = "~/images/"+FileUpload1.FileName;
                    this.FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);
                    this.Label1.Text = "ÎļþÉÏ´«³É¹¦";
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "Ô´Îļþ·¾¶£º" + this.FileUpload1.PostedFile.FileName;
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "Îļþ´óС£º" + this.FileUpload1.PostedFile.ContentLength + "×Ö½Ú";
                    this.Label1.Text += "<br/>";
                    this.Label1.Text += "<li>" + "ÎļþÀàÐÍ:" + this.FileUpload1.PostedFile.ContentType;
                }

                catch
                {
                    this.Label1.Text = "ÎļþÉÏ´«²»³É¹¦£¡";
                }

                finally
                {

                }
            }
            else

            {
                this.Label1.Text = "Ö»Äܹ»ÉÏ´«ºó׺ÃûΪjpg, gif, png, bmpµÄÎļþ";
            }
        }
    }
}


ÏÖÔÚ°Ñ´úÂë¸ÄºÃÁË£¬µ«ÊÇ×ÜÊÇÉÏ´«²»³É¹¦£¬ÓÖ²»ÖªµÀÄÄÀï´íÁË£¿  Çó½Ì~~~~~~~~
#5
shangsharon2013-07-31 22:04
ûÓÐÉÏ´«³É¹¦»¹ÊÇûÓб£´æ³É¹¦?ÓÐûÓб¨Ê²Ã´´í?
#6
Teem2013-08-01 10:57
»Ø¸´ 5Â¥ shangsharon
ûÓб¨´í£¬ Ñ¡ÖÐͼƬ¸ñʽ  ¿ÉÊÇÉÏ´«²»³É¹¦
#7
shangsharon2013-08-02 07:39
ÄÜ×ßµ½ÕâÒ»²½Âð?
this.FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);

ÉÏ´«²»³É¹¦ÊÇÕâ¸öÂð?
catch
{
this.Label1.Text = "ÎļþÉÏ´«²»³É¹¦£¡";
}
#8
·ÉÄêºÏ2013-08-06 11:41
ÄãÉÏ´«ºó£¬ÔÚÄã±£´æµÄ·¾¶Ï¿´ÊÇ·ñÓÐÄãÉÏ´«µÄÎļþ£¬Èç¹ûûÓеϰ£¬
°Ñ catch
                {
                    this.Label1.Text = "ÎļþÉÏ´«²»³É¹¦£¡";
                }

Óï¾ä¸Ä³É  catch (Exception ex)
                { this.Label1.Text = "ÎļþÉÏ´«²»³É¹¦¡£"; }
1