[英]Find the Directory of folder in root from a aspx page which is inside a folder in asp.net
I have a aspx page to upload image to a folder.The aspx page is inside a folder called admin. 我有一个aspx页面将图像上传到文件夹.aspx页面位于名为admin的文件夹内。 If i keep the aspx page in the root directory i am able to upload file in the Image folder since both are in the root directory. 如果我将aspx页面保留在根目录中,则由于两者都在根目录中,因此我能够在Image文件夹中上传文件。
But when i do the same keeping the aspx page inside a folder,it shows Could not find a part of the path 'C:\\Users\\...
但是当我做同样的事情将aspx页面保留在一个文件夹中时,它显示Could not find a part of the path 'C:\\Users\\...
So how do i provide path such that i am able to upload image in a folder from a page which is also inside a folder. 因此,我该如何提供路径,以便能够从文件夹内的页面上载文件夹中的图像。
Here goes my code: 这是我的代码:
if( fu_subcat.PostedFile.ContentLength>0 )
{
fn =Path.GetFileName(fu_subcat.PostedFile.FileName);
fu_subcat.SaveAs(Server.MapPath("imag/" + fn));
}
EDIT 编辑
It is mapping the path as: Could not find a part of the path 'C:\\Users\\lenovo\\Documents\\Visual Studio 2010\\WebSites\\emarket\\Admin\\imag
它将路径映射为: Could not find a part of the path 'C:\\Users\\lenovo\\Documents\\Visual Studio 2010\\WebSites\\emarket\\Admin\\imag
But i have the folder inside emarket as emarket\\imag
not emarket\\Admin\\imag
但是我在emarket内的文件夹为emarket\\imag
而不是emarket\\Admin\\imag
最好将图像保存在单独的内容文件夹中,然后使用tilda在相对路径中引用图像,例如〜“ content / image”
使用〜将使您进入应用程序的虚拟根目录,因此Server.MapPath(“〜/ imag / ...”)应该可以。
This happens because when you call that code from 发生这种情况是因为当您从
/Admin/page.aspx /Admin/page.aspx
Server.MapPath returns a path which is relative to /Admin/ - so the final path would be /Admin/imag/... To set path from the root you should add a / in front of the path. Server.MapPath返回相对于/ Admin /的路径-因此最终路径将为/ Admin / imag / ...要从根目录设置路径,应在路径前面添加/。
Must be 一定是
fu_subcat.SaveAs(Server.MapPath("/imag/" + fn));
Notice / in front of the string. 注意/在字符串前面。
Alternatively you can also add a ~ tilde 或者,您也可以添加〜波浪号
fu_subcat.SaveAs(Server.MapPath("~/imag/" + fn));
which points to a root of the current ASP.net application. 指向当前ASP.net应用程序的根。 This will be useful if you application is running in a virtual directory and as a part of global application. 如果您的应用程序在虚拟目录中运行并且作为全局应用程序的一部分运行,这将非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.