目录已创建,但文件未保存在文件夹中

本文关键字:保存 存在 文件夹 文件 创建 | 更新日期: 2023-09-27 17:57:43

在这里,用户创建一个文件夹,我想将文件保存在用户文件夹中。在创建文件夹之前我是成功的,但在创建文件夹之后,当我要保存文件时,它是不成功的。

错误显示

SaveAs方法配置为需要根路径

我的代码

 protected void Page_Load(object sender, EventArgs e)
{
    foreach (string s in Request.Files)
    {
        HttpPostedFile file = Request.Files[s];
        int fileSizeInBytes = file.ContentLength;
        string fileName = file.FileName;
        string fileExtension = "";
        if (!string.IsNullOrEmpty(fileName))
            fileExtension = Path.GetExtension(fileName);
            Guid UserGUID = (Guid)Membership.GetUser().ProviderUserKey;
            string UserFolderPath = "~/UploadedFiles/" + UserGUID;
            System.IO.Directory.CreateDirectory(Server.MapPath(UserFolderPath));
        //Upto this line it's OK.Below this,Not save the files inside the directory
       //I have no idea below 2 lines are correct or not
            string savedFileName = Path.Combine(UserFolderPath);
            file.SaveAs(UserFolderPath);
    }
}

目录已创建,但文件未保存在文件夹中

您需要将文件夹与文件名组合,以获得发送到SaveAs:的最终路径

string savedFileName = Path.Combine(Server.MapPath(UserFolderPath), fileName);
file.SaveAs(savedFileName);