如何动态地将物理路径转换为虚拟路径? [英] How to convert physical path to virtual path dynamically?

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

问题描述

到目前为止,这是我尝试过的工作,我将给定路径转换为虚拟路径,但是当我的网站不在同一文件夹中时,它将创建错误,因此我希望将转换为物理路径的代码转换为像这样的虚拟路径

物理路径:: C:\ Users \ NET DEVELOPMENT \ Desktop \ AgentWeb

我希望它能转换成这样的虚拟路径
虚拟路径::〜/AgentImages/

这里的代理图片是我的agentweb邮件文件夹中的一个文件夹,代码是这样的


This is what i have tried so far i am converting given path to virtual path but when my website is not in the same folder it will create error so i want a code that converts what ever the physical path is will be converted to virtual path like this

physical path:: C:\Users\NET DEVELOPMENT\Desktop\AgentWeb

and i want it to get converted to virtual path like this
virtual path:: ~/AgentImages/

here agent images is a folder in my agentweb mail folder and the code is like this


protected void btnSubmit_Click(object sender, EventArgs e)
   {
       int id = Convert.ToInt32(Session["AgentMasterID"]);
       string temp = "";
       if (fuProducImage.HasFile)
       {
           if (CheckFileType() == true)
           {

               string directoryPath = Server.MapPath("~/AgentImages/") + id;
               string vitualp = directoryPath.Replace(@"C:\Users\NET DEVELOPMENT\Desktop\AgentWeb", "~").Replace(@"\", "/");
               string filepath = directoryPath + "/" ;
               if (!Directory.Exists(MapPath(filepath)))
               {

                   Directory.CreateDirectory(MapPath(directoryPath + "/"));
                  fuProducImage.SaveAs(MapPath(filepath+fuProducImage.FileName));
                  temp = directoryPath + fuProducImage.FileName;
               }
               else
               {

                   fuProducImage.SaveAs(MapPath(filepath + fuProducImage.FileName));
                   temp = filepath + fuProducImage.FileName;
               }

               //fuProducImage.SaveAs(MapPath("~/AgentImages/" + fuProducImage.FileName));


               lblPimage.Visible = true;
               lblPimage.Text = "Image Uploaded Sucesfully";
           }
       }



请帮助我



pls help me on this

推荐答案

您首先必须了解并非所有物理路径都可以转换为虚拟路径,因为路径必须是当前应用程序根路径的子代!
如果是这样,您可以简单地从物理路径中切掉"根目录,而您有一个虚拟路径...
You first must to understand that not all physical paths can be converted to virtual path, for that the physical path MUST be a child of the current application''s root path!
If that is true you can simply ''cut'' the root from the physical path and you have a virtual path...
public string ReverseMapPath( string Path )
{
    string szRoot = Server.MapPath ( "~" );

    if ( Path.StartsWith ( szRoot ) )
    {
        return("~/" + Path.Replace ( szRoot, string.Empty ).Replace ( @"\", "/" ));
    }
}


看看您已发布的代码,您已开始使用虚拟路径(~/AgentImages/),并将其映射到相应的物理路径.然后,您尝试先将物理路径转换回虚拟路径,然后再将虚拟路径映射回物理路径.

您应该可以将其替换为以下内容:
Looking at the code you''ve posted, you''ve started with a virtual path (~/AgentImages/), and mapped it to the corresponding physical path. Then you try to convert the physical path back to a virtual path, before mapping the virtual path back to a physical path again.

You should be able to replace that with the following:
protected void btnSubmit_Click(object sender, EventArgs e)
{
    int id = Convert.ToInt32(Session["AgentMasterID"]);
    string temp = "";
    if (fuProducImage.HasFile)
    {
        if (CheckFileType() == true)
        {
            string directoryPath = Server.MapPath("~/AgentImages/") + id;
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            temp = Path.Combine(directoryPath, fuProducImage.FileName);
            fuProducImage.SaveAs(temp);

            lblPimage.Visible = true;
            lblPimage.Text = "Image Uploaded Sucesfully";
        }
    }
}


尝试以下代码示例.

Try below code sample.

public static string MapPathReverse(string fullServerPath)
    {
        return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath,String.Empty);
    }


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

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