.net上传文件

界面如下:


直接上代码


protected void btnUpload_Click(object sender, EventArgs e)
        {
            //新文件名
            string fileNewName = string.Empty;
            //文件模板类型
            string fileModelType = string.Empty;


            if (fileUpload.HasFile)
            {
                string ShortFileName = fileUpload.FileName;
                string fileType = Path.GetExtension(ShortFileName);
                string fileName = Path.GetFileNameWithoutExtension(ShortFileName);

                if (ShortFileName == "")
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>alert('请上传文件!');</script>");
                    return;
                }
                else
                {
                    //文件上传路径
                    string serviceFilePath = "../Upload/";
                    //文件命名格式
                    string fileTime = DateTime.Now.ToString("yyyyMMddhhmmss");

                    fileNewName = fileName + "-" + fileTime + fileType;
                    //附件保存路径
                    string filePath = Server.MapPath(serviceFilePath + fileNewName);
                    if (!Directory.Exists(Server.MapPath(serviceFilePath)))
                    {
                        Directory.CreateDirectory(Server.MapPath(serviceFilePath));
                    }
                    //保存文件
                    fileUpload.PostedFile.SaveAs(filePath);
                    labNewPath.Text = filePath;

                }
            }
        }

猜你喜欢

转载自blog.csdn.net/u012698249/article/details/79989275