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

时间:2014-04-09 09:35:59

标签: c# asp.net

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

错误显示

  

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);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要将文件夹与文件名相结合,以获取发送至SaveAs的最终路径:

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