ASPを実現する.NETマルチファイルアップロードプログラムコード
3954 ワード
upload.aspx
upload.aspx.cs
<br>function addFile()
<br>{
<br>var str = '<INPUT type="file" size="50" NAME="File">'
<br>document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
<br>}
<br>
upload.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebPortal
{
///
/// UpLoad 。
///
///
public class Upload : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus;
private void Page_Load(object sender, System.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;
}
}
#region Web
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: ASP.NET Web 。
//
InitializeComponent();
base.OnInit(e);
}
///
/// -
/// 。
///
private void InitializeComponent()
{
this.ID = "Upload";
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}