0

I have an asp.net mvc page (using C#) which contains a link containing a file name:

<a id="a1" href="#">File Name 1</a>

When this link is clicked, I'd like to pass the id of this file "a1" to the MVC Controller action using Ajax then the action should return back a file attachment (not accessible via file url but just a file attachment) so that the dialog with open/save button appears.

How to achieve this?

tereško
  • 58,060
  • 25
  • 98
  • 150
The Light
  • 26,341
  • 62
  • 176
  • 258
  • You don't need ajax for that, just provide a link to the file and let browser take care of the download; either, use `window.location`. – moonwave99 Oct 31 '13 at 11:34
  • like I said I can't do that, the link is local to the server inside e.g. c:/folder/file.doc. How can the server load that file and send it to the browser? – The Light Oct 31 '13 at 11:50
  • Put file outside webserver root, then let your controller read it and return to the client with proper headers. You have then to take care of protecting the temporary file URL somehow [authorization, token, sessions, etc...]. – moonwave99 Oct 31 '13 at 11:52
  • yeah that was the original question, how to implement that on the action side? to send an attachment back with headers, etc. – The Light Oct 31 '13 at 12:11
  • It depends on which technology are you using on your server, but you can split your problem in reading file on disk // set proper headers based on file MIME type // send answer back to client. – moonwave99 Oct 31 '13 at 12:20
  • Possible duplicate of http://stackoverflow.com/questions/1375486/how-to-create-file-and-return-it-via-fileresult-in-asp-net-mvc. – Alex Dresko Nov 01 '13 at 12:25

0 Answers0