Save selected part of web page as text file using Javascript - javascript

I have a web page with few tabs. All tabs have same data but in different format. The page has a copy to clipboard feature which copies the contents of the active tab to clipboard. Similarly we need to have a save as text file option which copies contents of the active tab alone by prompting use for selecting a location and then ssving it.
I checked a few alternatives like use of execcommand, but not sure whats best way to do the same.
Thanks in advance.

You could accomplish this by posting the contents to a server page which would basically echo the contents back in such a way as to provoke the browser into launching a "what do you want to do with this?" dialog where one option is "Save to disk." If there's a way to get the client to go straight to the "Save to disk" option, all the better.
I've found that different browsers deal with this in different ways - some look at the extension of the server page, and some at the HTTP content-type.
For example, I had a page, call it server.cgi, which would produce a zip file and I think IE defaulted the file name in the "Save As" dialog to server.cgi. By changing the server file to server.zip, the prompt did the right thing. I think Firefox responded to the content-type header.

Related

how to determine if the user click this download popup? [duplicate]

I got a webpage where members can download different kind of files on. I wan't to get information about which files and how many times each member have downloaded. when the user want to download a file he get browser pop-up where he gets 3 choises: "Open", "save" and "Cancel".(file dialog box in browser). i want to update the download status only if open/save button is clicked
Is there a way to detect which button was clicked on "save/open/cancel" dialog?
You can't know that. Some browsers even start the download before the user decides what to do with the file. But perhaps you can get your webserver to log if the download has been completed. That's probably the most reliable result you can get.
There is no way to know which button was clicked.
I dont think you can. The site just streams the file to the browser. What the user does
with the file is not known by the website as there is no more server side
interaction.

JavaScript to copy text

I want to create a javascript to copy text to clipboard and display an alert coupon copied and when user close it or clicks ok it should visit another website in new page.
basically i am using wordpress's siteorigin pagebuilder widget in which i can only provide the javascripct name as "script.js".
so what I want is when that script runs it takes the button id and copy its text to clipboard and then shows a error message and tells code copied and when user cancels the alert or click ok then it should open a link.
Currently, almost all browsers have prohibited access to the clipboard by javascript.
So, whatever you try here will fail due to browser restrictions. There are settings modifications which re-grant access to the clipboard, but they are not practicable in an end user scenario.
There are certain solutions around which are either based on a java plugin or a small flash app. The latter is the most stable and useful, can be found here:http://zeroclipboard.org/

Give an option to download pdf OR open in browser

I'm pretty sure this isn't possible but thought I'd check with you guys.
I've just had a bizarre query from a client who wants to know if it's possible to give the user the option to either download a pdf or open it in their browser.
They have a thumbnail for the PDF. Single click to open it in the browser or double click to download it. I know I can catch the single or double click with jQuery. I just want to know if it's possible to force the behaviour of the link.
Thanks
This is possible, if for example you are using PHP you can create 2 methods, once will be a simple direct link to the pdf file (if a customer has adobe reader then it will open the pdf in the browser otherwise it will force a download), the other method would always force a download.
You can find a PHP force file download example here: How to force download of a file?

iFrame file upload with IE produces download dialogue

I'm developing a custom module for Drupal dealing with Document Management. At this point, the module loads, you can upload files (via a hidden iframe and some ajax requests), browse directories and set various levels of permissions. And everything works perfectly in Firefox.
Issue:
In IE however, I run into an issue. For some reason when I upload a file the save file dialogue pops up and asks me to download the file I just uploaded. It looks like it's asking me to download it from the web-servers tmp location however, as that seems to be the file-name and such. However, if I hit cancel and refresh the page I can see that the file I uploaded did actually get uploaded to the server.
Here is how the upload process works. Click the upload button. The upload button is the standard file input form element hidden and placed over a styled version of the button. Clicking on this causes the "Choose a file" dialogue to open.
Select a file and click "Open". A modal dialogue pops up asking you for some further information about the file. The modal is part of the same form element but remains invisible until you click Upload button
Clicking save in the modal causes the file to be uploaded. The "action" attribute on the form is pointed to a page and the target is the iframe.
The iFrame is polled a few times every second to see if it's contents have changed. When the file is uploaded a "success" message appears in the iframe. Since it belongs to the same domain, I scrape the content within the iframe.
Once the iFrame says that the file has been uploaded, I use some JS to update the application with the name of the file
Since I can't use Firebug in IE, I have to stick to utilizing Microsofts Web Developer Toolbar, which makes it very hard to figure out if the bolded step is actually occuring. It seems like it should, since the file IS being uploaded. It is just getting interrupted by that file download dialogue.
Since no one seems to have an answer, I'll post the fix I found. I doubt it's perfect, but it's the best solution I could come up with.
It turned out to be an issue with the polling as suspected. The fix ended up being instead of outputting json to the iFrame, just output some JavaScript that calls a function that updates the main window. That simple.

Close browser popup page after downloading file

When user clicks on a hyperlink, i am opening a popup window which downloads a file. Post download, it shows open/save dialog box. I want to close the popup window once the download is done (when user is prompted for saving the file).
I have tried window.close method, but it doesnt work as the context is not the popup window but the open/save dialog box.
I think you can not control it programatically. This is browser-specific thing where some browsers allow you to tick on a check box to close the window and so on.
you can do one thing:
after the "file writing code in your servlet or struts action use"
Step 1: call "response.flushBuffer();"
Step 2: call response.sendRedirect("close.htm")
where close.htm is :
<html>
<script>
window.close();
</script>
</html>
Only problem is identifying if the download is complete. I don't think there is a possible way of doing that. Anyway is there any use of keeping the pop-up open untill the download is complete. You can simply close the pop-up once the download is initiated. Using below JavaScript, can't you?
pop_up= window.open("", "PopUpName");
pop_up.document.write('POPUP TEXT');
pop_up.close();
Browsers have a habit of deciding for themselves whether to try to download a file or open it inside the browser window, depending on the browser used, plug-ins and server settings. It sounds like you might be opening the link in a new window, as if the browser was going to open the file rather than download it, and then the browser has opted for the download. This leaves the user with a downloaded file and a blank window that you have no control over.
To force it to download, you should be able to set the Content-Type header for the target of the link to application/force-download. How you do this will depend on your setup, and whether the file is downloaded directly (in which case it will be a server setting) or via PHP or .Net (in which case it's easy to programmatically set the header). Also make sure that the hyperlink doesn't have a target="_blank" attribute that opens the link in a new window.
It's Browser functionality, You can change browser setting for that. You shouldn't go for programming . By default If you will download file in firefox it ask for saving, but in crome It's doesn't ask.
You need to change setting of your browser.

Categories

Resources