开发者

ASP.NET root paths

开发者 https://www.devze.com 2022-12-30 13:37 出处:网络
I am getting this exception when trying to save a file: System.Web.HttpException: The SaveAs method is configured to require a rooted path, and the path \'~/Thumbs/TestDoc2//small/ImageExtractStream.

I am getting this exception when trying to save a file:

System.Web.HttpException: The SaveAs method is configured to require a rooted path, and the path '~/Thumbs/TestDoc2//small/ImageExtractStream.bmp' is not rooted.
at System.Web.HttpPostedFile.SaveAs(String filename)
at System.Web.HttpPostedFileWrapper.SaveAs(String filename)
at PitchPortal.Core.Extensions.ThumbExtensions.SaveSmallThumb(Thumb image) in C:\Users\Bich Vu\Documents\Visual Studio 2008\Projects\PitchPortal\PitchPortal.Core\Extensions\ThumbExenstions.cs:line 23

The code is below:

public static void SaveSmallThumb(this  Thumb image)
    {
        var logger = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<ILoggingService>();

        string savedFileName = HttpContext.Current.Server.MapPath(Path.Combine(
          image.SmallThumbFolderPath,
          Path.GetFileName(image.PostedFile.FileName)));
        try
        {
            image.PostedFi开发者_如何学JAVAle.SaveAs(savedFileName);
        }
        catch (Exception ex)
        {
            logger.Log(ex.ToString());
        }
    }

What is wrong here?


You problem is your path being produce (possibly due to failure of MapPath):

~/Thumbs/TestDoc2//small/ImageExtractStream.bmp

Notice the 2 // between TestDoc2 and small.

You combine seems to be the issue that is probably causing the double slash.

What is the output of image.SmallThumbFolderPath and Path.GetFileName(image.PostedFile.FileName)?

The SaveAs requires a physical path (eg. c:/Thumbs/TestDoc2//small/ImageExtractStream.bmp).


It looks like MapPath isn't mapping your path properly. You probably have a / at the end of smallthumbfolderpath and at the begining of image.postedfile.filename, resulting in the double-slash in your resulting path. Remove the / from either one of these and the MapPath call should return properly.

The SaveAs method is expecting a rooted path, which means that it starts with a drive letter. Make sure that your path doesn't start with a ~ after you run MapPath and you should be fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号