【问题标题】:Webmatrix - WebImage helper and Create DirectoryWebmatrix - WebImage 助手和创建目录
【发布时间】:2013-12-11 16:51:46
【问题描述】:

我之前已经成功地使用 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 的名称创建一个目录。这工作正常。然后我尝试将新照片上传到该路径,我收到一条错误消息,提示“不支持给定路径的格式”。

有什么想法吗?

【问题讨论】:

  • 您可能需要“路径”才能拥有Server.MapPath(path)。我不确定,因为我只是看了一眼,但是每当我在这个环境中创建目录时,我都需要 Server.MapPath() 方法。

标签: razor webmatrix helper webimage


【解决方案1】:

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

imagePath = Path.Combine(path, MediumFileName);

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

【讨论】:

  • PS,我正在研究图像类型的标准化,所以文件扩展名稍后会发挥作用:p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多