简体   繁体   中英

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. 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.
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\\...
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

But i have the folder inside emarket as emarket\\imag not emarket\\Admin\\imag

最好将图像保存在单独的内容文件夹中,然后使用tilda在相对路径中引用图像,例如〜“ content / image”

使用〜将使您进入应用程序的虚拟根目录,因此Server.MapPath(“〜/ imag / ...”)应该可以。

This happens because when you call that code from

/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.

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. This will be useful if you application is running in a virtual directory and as a part of global application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM