首页 > 代码库 > .net C# FileUpload控件上传

.net C# FileUpload控件上传

代码

/// <summary>
        ///  保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPro_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
			  try
                {
					string Imgfile = string.Empty;
					if (FileUpload_ProductImg.HasFile)
					{
						string path = Server.MapPath("~/mImages/ProductUploadFile/");
						Imgfile = "mImages/ProductUploadFile/";
						//拼接文件的名称
						//DirectoryInfo dir = new DirectoryInfo(path);
						//文件名由文件夹中文件的数量和后缀组成
						//string name = dir.GetFiles().Length + Path.GetExtension(FileUpload_ProductImg.FileName);
						string name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + Path.GetExtension(FileUpload_ProductImg.FileName);
						Imgfile = Imgfile + name;
						FileUpload_ProductImg.PostedFile.SaveAs(path + name);
					}else
					{
					 Page.ClientScript.RegisterStartupScript(this.GetType(), "", @"<script>alert('没有选择文件,请先选择文件!');location.href=http://www.mamicode.com/'NewsContentList.aspx';</script>");>

.net C# FileUpload控件上传