0

We're using an Ajax based reporting package that I'm able to enhance with my own bits of jQuery/javascript. One of the problems we're encountering with this package is that it generates hyperlinks that look like:

<a href="//server/abcdefghijklmnopqrstuvwxyz.xlsx" target="_blank">

Since the client's browser doesn't know what to do with .xlsx files, it downloads them to their local drive. That's fine. The problem is that the client hates those long file names and would like to have them replaced with something shorter (for the sake of argument, let's say "abc.xlsx").

I can't just change the href because the server needs the long name to identify the data being returned, so what I need is a way to execute the GET using the original URL but then change the name of the returned file so that it is stored locally using the shorter name.

I know how to use jQuery to override the hyperlink's click function and use $.get() to obtain the data. What I don't know is what I should do once I've gotten the data to change it's apparent filename.

1
  • I should have added that our client is using Safari so that wonderful "download=" attribute isn't available. Commented Jan 24, 2014 at 14:40

3 Answers 3

1

For modern browsers, you can use the download attribute:

<a href="//server/abcdefghijklmnopqrstuvwxyz.xlsx" download="shortName.xlsx" target="_blank">
Sign up to request clarification or add additional context in comments.

4 Comments

This is a nice solution
The download attribute only works in Firefox and Chrome.
Figures, our client is using Safari.
@user2762423 - If they're using Safari, the only way to change it is server side. Since you seem to have no control over that, I'm afraid there's nothing you can really do :(
0

the tag have a attribute named "download", can you use like

<a href="//server/abcdefghijklmnopqrstuvwxyz.xlsx" download="abc.xlsx">Download</a>

In jquery must be something like

$('a').attr('download', 'abc.xlsx');

Comments

0

You can use attribute download with in anchor tag(a) which will only work in Firefox and Chrome. More info http://www.w3schools.com/tags/att_a_download.asp

But here is a server side script solution Change file name for download and start downloading it after click or delay

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.