问题描述
到目前为止,这是我尝试过的工作,我将给定路径转换为虚拟路径,但是当我的网站不在同一文件夹中时,它将创建错误,因此我希望将转换为物理路径的代码转换为像这样的虚拟路径
物理路径:: 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
推荐答案
public string ReverseMapPath( string Path )
{
string szRoot = Server.MapPath ( "~" );
if ( Path.StartsWith ( szRoot ) )
{
return("~/" + Path.Replace ( szRoot, string.Empty ).Replace ( @"\", "/" ));
}
}
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";
}
}
}
public static string MapPathReverse(string fullServerPath)
{
return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath,String.Empty);
}
这篇关于如何动态地将物理路径转换为虚拟路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!