ASP.NET上傳檔案的簡單例子

來源:互聯網
上載者:User

例子

 代碼如下 複製代碼

<html>
<title>ASP.NET簡單檔案上傳程式示範</title>
<body>
<script language=VB runat=server>
Sub uploadfile(sender as object,e as eventargs)
If fileup.postedfile.contentlength=0 Then
uptype.text="請選擇要上傳的檔案!!"
Else
Dim filesplit() as string=split(fileup.postedfile.filename,"")
Dim filename as string=filesplit(filesplit.length-1)
fileup.postedfile.saveas(server.mappath(".")&""&filename)
uptype.text="檔案名稱:"&fileup.postedfile.filename &"<br>"& _
"檔案大小:"&fileup.postedfile.contenttype &"<br>"& _
"檔案類型:"&fileup.postedfile.contentlength
End If
End Sub
</script>
<form enctype="multipart/form-data" runat=server>
<Input type=file id=fileup runat=server size="20"><p>
<asp:button id=upload_button onclick=uploadfile text="上傳" runat=server/>
</form>
<asp:label id=uptype runat=server/>
</body>
</html>

檔案上傳時可檢測檔案名稱、檔案大小和檔案類型,暫時沒有做上傳類型的限制,你可以自己添加類型限制,上面只是一個測試,下面我看一個更詳細的檔案上傳例子

 代碼如下 複製代碼

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)
    {

    }
    /// <summary>
    /// 上傳檔案到指定的檔案夾
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSure_Click(object sender, EventArgs e)
    {
        //string phName = this.txtName.Text;
        //string phType = this.ddlType.SelectedValue;

        if (this.myFile.PostedFile != null)  //myFile是上傳控制項的名稱
        {
            string photoName1 = myFile.PostedFile.FileName; //擷取初始檔案名稱
            int i = photoName1.LastIndexOf("."); //取得檔案名稱中最後一個"."的索引
            string newext = photoName1.Substring(i); //擷取副檔名
            if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
            {
                Response.Write("檔案格式不正確!");
                Response.End();
            }
            DateTime now = DateTime.Now; //擷取系統時間

            string classid = DateTime.Now.Year.ToString();

            //根據年份判斷在該路徑下是否存在以當年年份檔案夾 否則將建立以該年份的檔案夾
            if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "//" + classid))                //HttpContext.Current.Server.MapPath(相對路徑):把相對路徑轉為伺服器上的絕對路徑。File.Exists(絕對路徑):檢查是否存在絕對路徑指向的檔案或目錄。
            {
                System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "//" + classid);              //System.IO.Directory.CreateDirectory(檔案夾絕對路徑):建立絕對路徑檔案夾。
            }
            string photoName2 = now.Millisecond.ToString() + "_" + myFile.PostedFile.ContentLength.ToString() + newext; //重新為檔案命名,時間毫秒部分+檔案大小+副檔名
            myFile.PostedFile.SaveAs(Server.MapPath("photos//"+classid+"//" + photoName2)); // 儲存檔案到路徑,用Server.MapPath()取當前檔案的絕對目錄.在asp.net裡"/"必須用"//"代替
        }

    }
    /// <summary>
    /// 這是一個button控制項指定在某目錄下建立檔案夾的樣本,可供參考按照年月或不同的人上傳在不同的檔案夾裡
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCreateBag_Click(object sender, EventArgs e)
    {
        string classid = DateTime.Now.Year.ToString();
        if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "//" + classid))                //HttpContext.Current.Server.MapPath(相對路徑):把相對路徑轉為伺服器上的絕對路徑。File.Exists(絕對路徑):檢查是否存在絕對路徑指向的檔案或目錄。
        {
            System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "//" + classid);              //System.IO.Directory.CreateDirectory(檔案夾絕對路徑):建立絕對路徑檔案夾。

        }
        else
        {
            Response.Write("<script language=javascript>alert('檔案夾已經存在')</script>");
        }

    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.