1

我之前已经成功地使用 webimage 帮助程序上传了一个文件,但我现在正试图将它与创建一个目录结合起来,并且失败了。这是我的代码:

    if(IsPost){
    //Create Directory using PropertyID
    var imageroot = Server.MapPath("~/Images/Property/");
    var foldername =  rPropertyId.ToString();
    var path = Path.Combine(imageroot, foldername);
    if(!Directory.Exists(path)){
    Directory.CreateDirectory(path);
    }

    photo = WebImage.GetImageFromRequest();
    if(photo != null){
         MediumFileName = rPropertyId + "_" + gooid + "_" + "Medium";
         imagePath = path + MediumFileName;
         photo.Save(@"~\" + imagePath);}
}

首先,我创建一个名称为 propertyID 的目录。这工作正常。然后我尝试将新照片上传到该路径中,我收到一条错误消息,提示“不支持给定路径的格式”。

有任何想法吗?

4

1 回答 1

2

创建目录路径时正确使用Path.Combine(),制作图像路径时也应该这样做。

imagePath = Path.Combine(path, MediumFileName);

除此之外,错误消息表明可能是文件扩展名的遗漏导致了问题?也许使用Path.GetFileName(photo.FileName)或类似的并将其用作构造路径名的结尾。

于 2013-11-26T16:55:54.967 回答