Search code examples
razorwebmatrixhelperwebimage

Webmatrix - WebImage helper and Create Directory


I have previously successfully managed to upload a file using the webimage helper, but i am now trying to combine that with creating a directory, and failing miserably. here is my code:

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

First, i create a directory with the name of the propertyID. This works fine. I then try and upload new photo's into that path, and i get an error saying that "The given path's format is not supported".

Any ideas?


Solution

  • You correctly use Path.Combine() when creating the directory path, you should do the same when making the image path.

    imagePath = Path.Combine(path, MediumFileName);
    

    Other than that, the error message suggests that perhaps it is the omission of a file extension that is causing issues? Perhaps use Path.GetFileName(photo.FileName) or similar and use that as the end of your constructed pathname.