简体   繁体   English

从文件的根文件夹获取相对路径

[英]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 这是预期的工作结果(正确的路径): 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 但这就是MVC的作用: http:// localhost:60000 / ManualMeters /〜/ ManualMeterDocuments / client / Exports / client_0985-20160318-211554.xlsx

ManualMeters/ => the controller. ManualMeters / =>控制器。

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. 在控制器操作的一点上,我得到了物理路径:C:\\ Data \\ Test \\ source \\ Portal \\ Portal \\ ManualMeterDocuments \\ Client \\ Exports \\ Client_0985-20160318-214256.xlsx并尝试返回相对路径。

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. 这是从控制器返回到jquery ajax调用的var virtualFilePath中的值。 From there I bind it to a <a> href tag: ManualMeterDocuments\\Client\\Exports\\client_0985-20160318-215243.xlsx 从那里,我将其绑定到<a> href标记:ManualMeterDocuments \\ Client \\ Exports \\ client_0985-20160318-215243.xlsx

So that path is ok, but somehow somewhere ManualMeters/ is added to the link. 这样就可以了,但是以某种方式将ManualMeters /添加到了链接中。

EDIT: this is what I should be able to put in the href attribute to get the download working: test 编辑:这就是我应该能够在href属性中获得下载工作的作用:测试

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? 为什么用bs结束这个问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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