asp.Netファイルアップロード
3050 ワード
1.html
2.cs
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<input id="File1" runat="server" type="file" />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
<asp:Label ID="LblMsg" runat="server" Height="256px" Width="100%"></asp:Label>
</div>
</form>
</body>
</html>
2.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 other_ShangChuan1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ }
}
protected void Button1_Click(object sender, EventArgs e)
{
if (File1.PostedFile.FileName != "")
{
// :C:\12.jpg
string sFile = File1.PostedFile.FileName;
// :12.jpg
sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
// .jpg
sFile = sFile.Substring(sFile.LastIndexOf("."));
// ,
string datatime = System.DateTime.Now.ToString("yyyMMddHHmmssffff");
//
sFile = datatime + sFile;
//AppDomain.CurrentDomain.BaseDirectory.ToString()
//sPath
string sPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "uploads\\" + sFile;
//
File1.PostedFile.SaveAs(sPath);
this.LblMsg.Text = " :" + sPath;
this.LblMsg.Text += "<br/> :" + this.File1.PostedFile.FileName;
this.LblMsg.Text += "<br/> :" + this.File1.PostedFile.ContentType;
this.LblMsg.Text += "<br/> :" + this.File1.PostedFile.ContentLength + "Byte";
}
else
{
this.LblMsg.Text = " ?";
}
}
}