一個文件上傳的例子[轉]

UI
LEADBBS CODE

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title>FileUpload</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button  ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /><br />
        <asp:Image ID="imgFile" runat="server" ImageUrl="" Visible="false" /><br />
        <asp:Label  ID="lblMessage" runat="server"></asp:Label>
        
        
    </div>
    </form>
</body>
</html>





CS

LEADBBS CODE

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.HasFile) ///有文件執行操作
        {
            #region 檢測文件路徑,大小
            string strErr="";//錯誤信息
            int w=0;
            int h=0;
            if (this.FileUpload1.PostedFile == null)
            {
                strErr += "對不起,上傳文件不能爲空!//n";
            }

            int filesize = this.FileUpload1.PostedFile.ContentLength;
            if (filesize < 1)
            {
                strErr += "對不起,上傳文件不能爲空!//n";
            }
            if (filesize > 10485760)
            {
                strErr += "對不起,文件大小不能大於10M!//n";
            }

            strErr = strErr.Trim();
            if (strErr != "")
            {
                this.lblMessage.Text = strErr;
                return ;
            }
            #endregion

            #region 獲得文件信息
            string filename;
            string filetype;
            string savename;

            filetype = this.FileUpload1.PostedFile.ContentType.ToString();
            filename = this.FileUpload1.FileName;
            savename = System.DateTime.Now.ToString("yyyyMMddHHmmss") + filename;
            #endregion

            #region 檢測類型
            strErr = "";
            switch (filetype)
            {
                case "image/gif":
                case "image/bmp":
                case "image/pjpeg":
                    {
                        System.IO.Stream s = this.FileUpload1.PostedFile.InputStream;
                        System.Drawing.Image i = System.Drawing.Image.FromStream(s);
                         w = i.Width;
                         h = i.Height;


                        #region 判斷尺寸
                        //                                    if(this.lblWidth.Text!="")
                        //                                    {
                        //                                          if(w>int.Parse(this.lblWidth.Text))
                        //                                          {
                        //                                                strErr+="對不起,該文件格式超過規定寬度!//n";                                                
                        //                                          }
                        //                                    }
                        //                                    if(this.lblHigh.Text!="")
                        //                                    {
                        //                                          if(h>int.Parse(this.lblHigh.Text))
                        //                                          {
                        //                                                strErr+="對不起,該文件格式超過規定高度!//n";                                                      
                        //                                          }
                        //                                    }            
                        #endregion
                    }
                    break;
                //case "application/x-shockwave-flash":
                //case "application/octet-stream":
                //    break;
                //case "video/x-ms-wmv":
                //case "video/mpeg":
                //case "video/x-ms-asf":
                //case "video/avi":
                //case "audio/mpeg":
                //case "audio/mid":
                //case "audio/wav":
                //case "audio/x-ms-wma":
                //    break;
                //case "application/vnd.rn-realmedia-vbr":
                //    break;
                default:
                    strErr += "對不起,不允許該文件格式上傳!//n";
                    break;
            }
            if (strErr != "")
            {
                this.lblMessage.Text = strErr;
                return;
            }
            #endregion

            string strInfo="";
           
            strInfo += "文件名:" + filename + "<br>";
            strInfo += "文件大小:" + filesize + "<br>";
            strInfo += "文件類型:" + filetype + "<br>";
            strInfo += "文件高度:" + h + "<br>";
            strInfo += "文件寬度:" + w + "<br>";

            string savepath = "UpPic" + "//" + savename;
            string fullpath = Server.MapPath("UpPic") + "//" + savename;
            this.FileUpload1.SaveAs(fullpath);
           
            this.lblMessage.Text = strInfo;
            this.imgFile.ImageUrl = savepath;
            this.imgFile.Visible = true;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章