Jquery Download A File

Posted By admin On 12/01/22

Tip: Place the downloaded file in the same directory as the pages where you wish to use it. JavaScript is the default scripting language in HTML5 and in all modern browsers! If you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery Network). Both Google and Microsoft host jQuery. Hi, I am using ASP.NET and C# 4.0. In my web application, i am trying to implement a download file functionality. I have tried the below, but the file is not getting downloaded. May 12, 2014  Hi, I am using ASP.NET and C# 4.0. In my web application, i am trying to implement a download file functionality. I have tried the below, but the file is not getting downloaded. See here for a similar post on using jQuery to clear forms: Resetting a multi-stage form with jQuery You may also be running into an issue where the values are being repopulated by the struts value stack.

Active2 years, 1 month ago

How can I prompt a download for a user when they click a link.

For example, instead of:

I could use:

This way, Google does not index my HREF's and private files.

Can this be done with jQuery, if so, how? Or should this be done with PHP or something instead?

RedBlueThing
34.8k17 gold badges89 silver badges116 bronze badges
DodinasDodinas
2,93020 gold badges66 silver badges101 bronze badges

10 Answers

I might suggest this, as a more gracefully degrading solution, using preventDefault:

Even if there's no Javascript, at least this way the user will get some feedback.

karim79karim79
306k58 gold badges390 silver badges392 bronze badges

If you don't want search engines to index certain files, you can use robots.txt to tell web spiders not to access certain parts of your website.

If you rely only on javascript, then some users who browse without it won't be able to click your links.

RobRob

Here's a nice article that shows many ways of hiding files from search engines:

JavaScript isn't a good way not to index a page; it won't prevent users from linking directly to your files (and thus revealing it to crawlers), and as Rob mentioned, wouldn't work for all users.
An easy fix is to add the rel='nofollow' attribute, though again, it's not complete without robots.txt.

KobiKobi
112k35 gold badges231 silver badges266 bronze badges

Yes, you would have to change the window.location.href to the url of the file you would want to download.

MacAnthonyMacAnthony
3,8482 gold badges18 silver badges26 bronze badges
EL missaoui habibEL missaoui habib
8131 gold badge11 silver badges23 bronze badges

By stating window.location.href = 'uploads/file.doc'; you show where you store your files. You might of course use .htacess to force the required behaviour for stored files, but this might not always be handful....

It is better to create a server side php-file and place this content in it:

This code will return ANY file as a download without showing where you actually store it.

You open this php-file via window.location.href = 'scripts/this_php_file.php?f=downloaded_file';

Daniel
3,2492 gold badges31 silver badges57 bronze badges
OtvazhniiOtvazhnii
Community
letronje

Jquery Download File Without Redirect

letronje
5,2436 gold badges38 silver badges48 bronze badges
Ganganath RathnasekaraGanganath Rathnasekara

I suggest you use the mousedown event, which is called BEFORE the click event.That way, the browser handles the click event naturally, which avoids any code weirdness:

Downloadlingling
4,5043 gold badges32 silver badges32 bronze badges

See here for a similar post on using jQuery to clear forms: Resetting a multi-stage form with jQuery

You may also be running into an issue where the values are being repopulated by the struts value stack. In other words, you submit your form, do whatever in the action class, but do not clear the related field values in the action class. In this scenario the form would appear to maintain the values you previously submitted. If you are persisting these in some way, just null each field value in your action class after persisting and prior to returning SUCCESS.

Community
Russell ShingletonRussell Shingleton

Jquery Download File From Server

2,8711 gold badge16 silver badges29 bronze badges

Jquery Download File From Url And Save

protected by karim79Aug 9 '11 at 1:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged jqueryfiledownload or ask your own question.