ASP.NET 2.0 - 如何把上传的文件保存到 服务器某个目录中

Boolean fileOK = false; //文件上传是否OK
        String path = Server.MapPath("~/Activity/Uploaded_BudgetFile/"); //设定存储路径为当前站点根目 录下的一个文件夹UploadedImages
        if (this.FileUpload_BudgetFile.HasFile) //如果FileUpload中有上传文件
        {
            String fileExtension = System.IO.Path.GetExtension(FileUpload_BudgetFile.FileName).ToLower();
            if (fileExtension == @".xls")
            {
                fileOK = true;
            }
        }

        if (fileOK)
        {
            try
            {
                FileUpload_BudgetFile.PostedFile.SaveAs(path + FileUpload_BudgetFile.FileName);

            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(typeof(string), "Error", "<script>alert('上传文件出错!');</script>");
                return;
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(typeof(string), "Error", "<script>alert('上传文件必须为Excel文件!');</script>");
            return;
        }

posted @ 2006-12-29 14:13  RobotTech  阅读(1850)  评论(0编辑  收藏  举报