.net文件上传
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication3
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string _namepath = "/Files";
{
string _namepath = "/Files";
HttpPostedFile files = Request.Files["Upfile"];
string tpname = Guid.NewGuid().ToString() + new FileInfo(files.FileName).Extension;
string tpath = filename(Server.MapPath(_namepath));
string Jpath = tpath + "\\" + tpname;
string Xpath = _namepath + "/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + tpname;
Upfile.SaveAs(Jpath);
Response.Write(Xpath);
}
//创建文件夹
public string filename(string Namepath)
{
string year = DateTime.Now.Year.ToString();
string month = "";
string day = "";
if (Convert.ToInt32(DateTime.Now.Month.ToString()) > 10)
{
month = "0" + DateTime.Now.Month.ToString();
}
else
{
month = DateTime.Now.Month.ToString();
}
if (Convert.ToInt32(DateTime.Now.Day.ToString()) > 10)
{
day = "0" + DateTime.Now.Day.ToString();
}
else
{
day = DateTime.Now.Day.ToString();
}
string path = Namepath + @"/" + year + @"/" + month + @"/" + day;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
public string filename(string Namepath)
{
string year = DateTime.Now.Year.ToString();
string month = "";
string day = "";
if (Convert.ToInt32(DateTime.Now.Month.ToString()) > 10)
{
month = "0" + DateTime.Now.Month.ToString();
}
else
{
month = DateTime.Now.Month.ToString();
}
if (Convert.ToInt32(DateTime.Now.Day.ToString()) > 10)
{
day = "0" + DateTime.Now.Day.ToString();
}
else
{
day = DateTime.Now.Day.ToString();
}
string path = Namepath + @"/" + year + @"/" + month + @"/" + day;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
protected void Button2_Click(object sender, EventArgs e)
{
//FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
}
}
}
{
//FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
}
}
}
<asp:FileUpload ID="Upfile" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传" />
<br />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />