Javascript Check whatever link is download link or webpage link - javascript

It there anyway to check link whatever is a download link like http://ipv4.download.thinkbroadband.com/5MB.zip
or normal web like open https://www.google.com
Currently I am using Electron and want to check if link is download link it will download and not open new browser but if not it will open in browser window

Make an HTTP request and check the Content-Type and (if it is there) Content-Disposition headers.
If Content-Disposition says it is an attachment, then it is a download. If it says it is inline, then it is intended to be displayed in the browser window.
If Content-Disposition isn't specified, then you'll need to make the decision based on the MIME type. e.g. text/html should probably be shown in the browser window.

May be check the end of the link... if it ends with .zip .jpg .pdf .... it is a file.
Or .html ... it is a page.

Related

Need to download a file from webpage to local drive

I has a requirement like when user clicks on a download button instead of showing the content in browser, i want to save it to the localdisk(perticular location) of the user desktop. Is it possible to do??
If yes,Please help me with possibilities..
Thanks in advance
No a website can't decide where it can save something. Everything goes to download folder by default. You have to be using some sort of plugin with permissions or make like browser addon/extension.
If you want to prompt download then you could set send headers in php:
Content-Disposition: attachment; filename="fname.ext"
and
Content-Type: application/force-download
Or you could set attribute download to link in html
<a href="file.abc" download>Click Me</a>
Utility of HTTP header "Content-Type: application/force-download" for mobile?
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
http://www.php.net/manual/en/function.header.php
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes
You shall just point the file name like this to download.
But you cannot decide the path by your code.
Download the File
Note : Do not try this in jsfiddle or in codepen because they will rename the link with their custom so that the file will be displayed within their output. So, try it in your web server or in your localhost.

detecting Download file dialog?

is there any way i could detect opening of download file dialog box on web pages like hyperlink click event occurs and download file dialog box appears.... ??
and can i edit the filename in it ...... like attaching some website name along with filename .... so when the user download any file it automatically rename in to website-filename.pdf etc pro-grammatically
can we use input tag for it ?? or have to make customcontrols for it ??
thanks if any help provided
take care.
regards,
newbiefreak
You can just make a hyperlink with its href to a regular file, your browser will prompt to download it.
As for renaming the file, all you could do is create a special page which sends the file contents and correct headers, and specifying another name. You'll have to send the content-disposition header, as such:
Content-disposition: attachment; filename=yourfilename.extension
You can send a Content-disposition header to force a file downlaod box and specify a default filename.
http://support.microsoft.com/kb/260519
In regards to editing the filename:
HTML5 introduces a new attribute for a tags: download.
Using it forces browsers that support the attribute to prompt for a file download, rather than navigating to or attempting to open the linked file.
Also, whatever you value you assign to download will replace the file's actual name.
Source and demo: http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download

Standard way of doing a "thanks_for_downloading.html" + "Save file..." after a link?

Let's look at FireFox.
They have a nice big Call To Action button: "Firefox 3.6 - Free Download"
You click that, it links you to a new page: "Thanks for downloading Firefox! Your download should begin in a few seconds."
Then a few seconds later up pops: "FIREFOX.exe - Do you want to save or discard this file?"
This is pretty standard download behavior for applications accross the web. How is it done, in the simplest way?
There are two parts to this:
Forcing the browser to download the file, rather than trying to display it (with .exe that's no problem, but you might want to do it with an image, movie, or HTML file too).
Prompting the browser to download the file.
Let's pretend we just want the browser to download a file, without wanting to change the page. We can create a link like this, and it'll work as expected:
Download File
Your browser probably doesn't know how to handle a zip file, so it downloads "download.zip" straight to a file named after the filename in the URL. If you wanted to download a JPG instead, though, this wouldn't work:
Save this Sunset!
Your browser knows how to display a JPEG, so it redirects the page and shows the JPEG. Now we need to tell the browser not to show it, but to download it instead. To do that, we have to send it some specific HTTP headers in the response from the server.
Apache can handle this by specifying headers in .htaccess, but I'll stay away from a particular technology, opting to just talk about the mechanism.
So we send the following header to the browser along with the image:
Content-disposition: attachment; filename=the_sunset_of_a_lifetime.jpg;
The first header, content-disposition, tells your browser that we want the file to be an attachment, or in other words, it should be saved, not displayed. The filename attribute tells it the name to use to save the file (rather than "sunset.jpg", the file will be named "the_sunset_of_a_lifetime.jpg").
Now the link to download the "sunset.jpg" file works like we want. But how do we get the browser to download it without the user clicking on the link so we can show a "Thank You" page and prompt the download to start? A simple <meta> tag can do the trick, telling the browser to redirect the page after a set period of time:
<meta http-equiv="refresh" content="2;url=/images/sunset.jpg">
When your "thank you" page loads with that meta tag in the head, it'll wait for 2 seconds and then try to load the image. It'll get the headers we set in the last step, and download it instead of displaying it, and the user will stay put on the page like we want.
Example content of thanks_for_downloading.html:
<strong>Thanks for downloading XY</strong>
<script type="text/javascript">
window.onload = function(){
window.location.href = "path/to/XY.exe";
};
</script>
The Download-link is just a link to the thanks_for_downloading.html page
The call to action button is a regular link to the "Thank you" page. Then on the "Thank you" page, use javascript to redirect the user to the file you are downloading by setting the "window.location" property to the file's URL.

Using Javascript to send an HTTP attachment to the user (open browser's Save as... dialog)

I'm new to web development, so I apologize if this question is noobish. I want to serve a file that is on the server's hard drive to the user when requested (ie, send an HTTP attachment to trigger the browser's "Save as..." dialog) in Javascript. The user clicks on a button on the page, the server generates a customized data file based on some of his/her account settings (and other parameters), and then the "Save as..." dialog should pop up. How should I go about implementing this in Javascript?
edit: for your reference, the server has Glassfish and Apache
Jane,
The save-as dialog only appears on page load. You need to redirect your user either directly to the file you want them to save, or to a server-side page that serves up the file.
Once you know the address of the file, do something like
window.location = http://yourserver.com/generatedfiles/file_2342342.txt
Alternatively, do something like this:
window.location = http://yourserver.com/getgeneratedfile.aspx?fileID=2342342
...which would redirect the user to a page that feeds the generated file. You then need to specify the content-disposition and filename in the header that comes from that page, as mentioned in Gandalf's reply.
Edit: Ah, you're using Apache. Probably won't have ASPX files on there then.
Set the Http Response header:
Content-Disposition: attachment; filename=myfile.txt
Or something like this
Save this page
#aric's answer is correct; however, window.location will cause load/unload events to get fired which may not be desirable for your application. In this case, you can likely direct a hidden iframe to the url to cause the save dialog to appear without losing your page's state.
Also, 'SaveAs' is probably an IE specific value for document.execCommand as it doesn't exist in Firefox.

in javascript how can I detect if a browser will display or download a pdf?

Say I have:
a webpage with an iframe: <iframe src="" style="display:none;"></iframe>
an URL pointing to a PDF document: http://www.example.com
some javascript that will do iframe.src = pdfurl
a button that will trigger such javascript
if the browser is going to display the PDF inline, the button will
say "view pdf" and when clicked will make the iframe visible
otherwise it will say "download pdf"
I found a way to detect whether the pdf has been loaded in the iframe: reading iframe.contentDocument.contentType after onload has fired, but
this won't allow me to display the correct button
onload does not fire if the file is being downloaded
Thanks :)
To tell the client's browser to download a response as a file, set the Content-Disposition HTTP header to 'attachment' in your response. This is no guarantee, but it's the proper method of telling the browser how to handle the content.
ยง 8.9.1.6 PDF viewing support
window.navigator.pdfViewerEnabled
Returns true if the user agent supports inline viewing of PDF files when
navigating to them, or false otherwise. In the latter case, PDF files
will be handled by external software.
MDN web docs article.
Browser compatibility table
In modern Browsers, JavaScripts global Navigator object has a plugins property, filled with an array of Plugins, if you can find a Plugin for Mimetype application/pdf, you can safely assume that the browser will display pdf files inline, as long as the server does not explicit send content-disposition: attachment headers, of course.
You could send a HEAD request to that resource and check what Content-Type and Content-Disposition values are being sent back. Based on these information you could determine whether a browser would display or download that resource.

Categories