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

3
  • what is the value coming in exportFile? Commented Mar 18, 2016 at 21:17
  • 2
    Possible duplicate of Getting relative virtual path from physical path Commented Mar 18, 2016 at 21:20
  • This is the value in exportFile: C:\Data\Test\source\Portal\Portal\ManualMeterDocuments\Client\Exports\Client_0985-20160318-214256.xlsx Commented Mar 18, 2016 at 21:22

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.