从虚拟路径物理路径转换 [英] convert from physical path to virtual path

查看:517
本文介绍了从虚拟路径物理路径转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能,获取的FILEDATA作为字节数组和一个文件路径。当它试图设置的fileInfo在code bewlo我得到的错误是。它说,'物理路径给出,预计虚拟路径

 公共覆盖无效WriteBinaryStorage(字节[] FILEDATA,字符串文件路径)
    {
        尝试
        {
            //如果不存在,创建目录。
            System.IO.FileInfo的fileInfo =新System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(文件路径)); //当它到达这条线被捕获错误
            如果(!fileInfo.Directory.Exists)
            {
                fileInfo.Directory.Create();
            }            //写的二进制内容。
            System.IO.File.WriteAllBytes(System.Web.HttpContext.Current.Server.MapPath(文件路径),FILEDATA);
        }
        赶上(例外)
        {
            扔;
        }
    }

在调试它,将提供的文件路径为E:\\\\ \\\\腹板的webapp \\\\ \\\\默认图像\\\\ \\\\电源myImage.jpg这个参数。和错误消息是

 'E:/WEBS/webapp/default/images/mains/myimage.jpg是物理路径,但预计虚拟路径。

此外,它是什么触发这​​种情况发生的以下调用

  properties.ResizeImage(imageName,Configurations.ConfigSettings.MaxImageSize,使用Server.Mappath(Configurations.EnvironmentConfig.LargeImagePath));


解决方案

如果你已经有了一个物理路径,它没有意义调用使用Server.Mappath

您正在呼叫的MapPath 两次。

I have this function that gets the fileData as a byte array and a file path. The error I am getting is when it tries to set the fileInfo in the code bewlo. It says 'Physical Path given, Virtual Path expected'

 public override void WriteBinaryStorage(byte[] fileData, string filePath)
    {
        try
        {
            // Create directory if not exists.
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePath)); //when it gets to this line the error is caught
            if (!fileInfo.Directory.Exists)
            {
                fileInfo.Directory.Create();
            }

            // Write the binary content.
            System.IO.File.WriteAllBytes(System.Web.HttpContext.Current.Server.MapPath(filePath), fileData);
        }
        catch (Exception)
        {
            throw;
        }
    }

When debugging it, is providing the filePath as "E:\\WEBS\\webapp\\default\\images\\mains\\myimage.jpg" . And the error message is

'E:/WEBS/webapp/default/images/mains/myimage.jpg' is a physical path, but a virtual path was expected.

Also, what it is triggering this to happen is the following call

properties.ResizeImage(imageName, Configurations.ConfigSettings.MaxImageSize, Server.MapPath(Configurations.EnvironmentConfig.LargeImagePath));

解决方案

If you already have a physical path, it doesn't make sense to call Server.MapPath.

You're calling MapPath twice.

这篇关于从虚拟路径物理路径转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆