My requirement is to download a file when "Download" button is clicked. Service is giving me the url of the file. I used window.location but it is replacing my page. I just want the default download dialog provided by browser. How do I get that? I am getting a string (file location) from service, not the file. I am using jQuery.
Asked
Active
Viewed 209 times
0

Rosy
- 31
- 3
-
Have you tried opening a new window pointed to that url? – Jason P Sep 17 '13 at 20:59
-
Yes, it opens a new window with file contents. – Rosy Sep 17 '13 at 21:07
-
1I would change your code a bit. I would simply open the window to `/Controller/GetFile?ID=1` and have that action return a `FileResult`. – Jason P Sep 17 '13 at 21:12
-
Tried that, got "http:/someOtherServer/file.txt is not a valid virtual path" error. The file is on a different server but I do have access. If I give url to window.location as shown above, it's opening the file. – Rosy Sep 18 '13 at 14:25
-
Is the file hosted on a different server? – Jason P Sep 18 '13 at 14:33
-
Yes and it's a zip file. – Rosy Sep 18 '13 at 15:04
-
In that case, I think its up to the other server how the file is served. Another alternative would be to read the file using code in your controller, and pass the byte array to the FileResult and return that to the user. Not sure how well that would work. – Jason P Sep 18 '13 at 15:19
-
This might help you http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – Pitamber Tiwari Sep 18 '13 at 21:07
-
Thanks for responses but my code worked fine when the server sends zip file. I had been trying with txt files and browser was opening the file itself in the browser window. – Rosy Sep 18 '13 at 22:03