<form method="post" enctype="multipart/form-data" action="Default2.aspx"> <div> <input type="file" name="img" /><input type="submit" value="提交" /></div> </form>
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection files = Request.Files;
string fileDestination = string.Empty;
if (files.Count > 0)
{
HttpPostedFile postedFile = Request.Files[0];
string fileName = Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
SaveFile(postedFile, "IMAGE");
Response.Write("upload succes!");
}
}
}
public string SaveFile(HttpPostedFile postedFile, string postType)
{
string UploadFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
string extension = Path.GetFileName(postedFile.FileName);
string strFileName = HttpContext.Current.Request.ApplicationPath + "//UpLoadFile//" + postType + "//" + System.DateTime.Today.Year.ToString() + "//" + System.DateTime.Today.Month.ToString() + "//";
string root = System.Web.HttpContext.Current.Server.MapPath(strFileName);
string destination = System.Web.HttpContext.Current.Server.MapPath(strFileName) + UploadFileName + extension;
string path = "/UpLoadFile/" + postType + "/" + System.DateTime.Today.Year.ToString() + "/" + System.DateTime.Today.Month.ToString() + "/" + UploadFileName + extension;
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
postedFile.SaveAs(destination);
return path;
}
浙公网安备 33010602011771号