简体   繁体   English

从aspx.net文件夹内的aspx页面中找到根目录下的文件夹目录

[英]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.

相关问题 来自区域中控制器的ASP.NET MVC [Authorize]在根文件夹中找不到Account / Login ActionResult - ASP.NET MVC [Authorize] from controller in area can't find Account/Login ActionResult in root folder 如何从asp.net的父aspx页中找到保留在另一个usercontrol中的用户控件? - How to find user control kept inside another usercontrol from parent aspx page in asp.net? 根文件夹的asp.net处理程序 - asp.net handler for root folder 如何使用Response.Redirect从子文件夹重定向到ASP.NET中的根目录,防止用户查看目录列表 - How to use Response.Redirect from sub folder to root in ASP.NET, Prevent user from viewing directory listing 将/ folder重定向到ASP.NET和IIS 6上的/mypage.aspx - Redirect /folder to /mypage.aspx on ASP.NET and IIS 6 URL 在条件 ASPX、ASP.Net 的情况下无需更改文件夹即可重写 - URL rewrite without folder change on condition ASPX , ASP.Net 我如何进入asp.net网站的根目录? 或返回文件夹? - How do I get to the root directory of an asp.net website? or go back a folder? ASP.Net视图文件夹,其中包含一个文件夹-如何创建控制器 - ASP.Net view folder with a folder inside - How to create the controller 如何在asp.net C#应用程序中打开保存在本地目录/文件夹中的文件 - How to open file which saved on local directory/ folder in asp.net C# application 无法通过asp.net根目录下子文件夹下的网页打开文件 - Could not open file through web page which is under subfolder under root directory in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM