天天看點

.net html檔案上傳,asp.net(c#)實作檔案上傳功能

UpLoad.aspx

多檔案上傳

function addFile()

{

var str = ' '

document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)

}

多檔案上傳

Width="500px" BorderStyle="None" BorderColor="White">

UpLoad.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

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;

public partial class WEB_Test_UpLoad : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

/// 在此處放置使用者代碼以初始化頁面

if (this.IsPostBack) this.SaveImages();

}

private Boolean SaveImages()

{

///'周遊File表單元素

HttpFileCollection files  = HttpContext.Current.Request.Files;

/// '狀态資訊       System.Text.StringBuilder strMsg = new System.Text.StringBuilder();       strMsg.Append("上傳的檔案分别是:

");       try       {         for(int iFile = 0; iFile < files.Count; iFile++)         {           ///'檢查檔案擴充名字           HttpPostedFile postedFile = files[iFile];           string fileName, fileExtension;           fileName = System.IO.Path.GetFileName(postedFile.FileName);           if (fileName != "")           {             fileExtension = System.IO.Path.GetExtension(fileName);             strMsg.Append("上傳的檔案類型:" + postedFile.ContentType.ToString() + "

");             strMsg.Append("用戶端檔案位址:" + postedFile.FileName + "

");             strMsg.Append("上傳檔案的檔案名:" + fileName + "

");             strMsg.Append("上傳檔案的擴充名:" + fileExtension + " ");             ///'可根據擴充名字的不同儲存到不同的檔案夾             ///注意:可能要修改你的檔案夾的匿名寫入權限。             postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);           }         }         strStatus.Text = strMsg.ToString();         return true;       }       catch(System.Exception Ex)       {         strStatus.Text = Ex.Message;         return false;       }     }   }