3

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

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
PitAttack76
  • 2,120
  • 5
  • 31
  • 44
  • what is the value coming in ``exportFile``? – Ehsan Sajjad Mar 18 '16 at 21:17
  • 2
    Possible duplicate of [Getting relative virtual path from physical path](http://stackoverflow.com/questions/6081433/getting-relative-virtual-path-from-physical-path) – Ehsan Sajjad Mar 18 '16 at 21:20
  • This is the value in exportFile: C:\Data\Test\source\Portal\Portal\ManualMeterDocuments\Client\Exports\Client_0985-20160318-214256.xlsx – PitAttack76 Mar 18 '16 at 21:22

1 Answers1

1

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?

PitAttack76
  • 2,120
  • 5
  • 31
  • 44