简体   繁体   中英

get relative path from root folder for a file

I have a problem to create a correct download link for a file.

This is the expected and working result (correct path): http://localhost:60000/ManualMeterDocuments/client/Exports/client_0985-20160318-211554.xlsx

But this is what MVC makes of it: http://localhost:60000/ManualMeters/~/ManualMeterDocuments/client/Exports/client_0985-20160318-211554.xlsx

ManualMeters/ => the controller.

In my controller action at a point I get the physical path: C:\\Data\\Test\\source\\Portal\\Portal\\ManualMeterDocuments\\Client\\Exports\\Client_0985-20160318-214256.xlsx and try to return the relative path.

I've tried different things but can't get it right.

var virtualFilePath = exportFile.Replace(Server.MapPath("~"), "~/");
return Json(virtualFilePath, JsonRequestBehavior.AllowGet);

This is the value in the var virtualFilePath returned from the controller to jquery ajax call. From there I bind it to a <a> href tag: ManualMeterDocuments\\Client\\Exports\\client_0985-20160318-215243.xlsx

So that path is ok, but somehow somewhere ManualMeters/ is added to the link.

EDIT: this is what I should be able to put in the href attribute to get the download working: test

This fixed it: ..\\

 var virtualFilePath = Path.Combine(@"..\" + exportFile.Replace(Server.MapPath("~"), ""));

Returns:

http://localhost:60000/ManualMeterDocuments/Client_0985/Exports/client_0985-20160318-224049.xlsx

And why the bs to close this question?

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