Bookmarklet to save file - firefox

Is is possible to use a bookmarklet in Firefox to save/open a file directly?
Many bookmarklets open a page on which one can click on a link to download the result. E.g. using a blob link. Is it possible to avoid this extra click and invoke the "save file" / "open file" dialog directly?

To trigger the "Save As" dialog for any resource (blob:, http:, whatever permitted scheme), use the download attribute of an anchor. This is supported since Firefox 20.
Example: A bookmarklet that presents the current page as a download:
javascript:(function() {
var a = document.createElement('a');
a.href = location.href;
a.download = 'filename.html';
document.body.appendChild(a);
a.click();
a.parentNode.removeChild(a);
})();
To trigger the Open dialog, create an <input type="file">, and click() it. For many examples, see Using files from web applications.

Should be possible if the bookmarklet sends you to a page where the web server sends the appropriate headers to force a download. Example:
Content-Disposition: attachment; filename="filename.zip"
Content-Type: application/zip

Related

Download (TIFF) instead of opening by default in Internet Explorer

We are using Internet Explorer 11. In one of our webpage we got an anchor link which is pointing to a TIFF file. When I click the anchor link by default it is opening in the same page. We want to download (dialog box to save) instead of opening in the same window.
Is there any configuration available in IE?
See the Content-Disposition (RFC-2616) HTTP header, and use the value attachment (as opposed to the default value; inline):
If this header is used in a response [...] the user agent should not display the response, but directly enter a `save response as...' dialog.
Example:
Content-Disposition: attachment; filename="filename.tif"
See also Content-Disposition (MDN):
The first parameter in the HTTP context is either inline (default value, indicating it can be displayed inside the Web page, or as the Web page) or attachment (indicating it should be downloaded; most browsers presenting a 'Save as' dialog, prefilled with the value of the filename parameters if present).

"Save As..." won't work in IE8

Part of the functionality of my intranet application is that a user needs to see some XML which has been generated. A new browser tab is opened, and the WebAPI controller streams XML.
This works fine, problem is when the user clicks on "Save As...", nothing happens.
If I place a static xml file on the server, browse to it and click "Save As..." then this works fine; so I don't see this as being caused by a group policy or desktop build error. This also works fine in a proper web browser.
The code to return the xml...
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(fpml, Encoding.UTF8, "text/xml"),
};
If you trick IE into thinking that it has a file, rather than dynamic content, then the "Save As..." feature will work as expected.
This can be done by simply adding a filename into the URL, i.e.
Instead of
http://myhost/myapp/api/getFpmlApi/?TradeId=234234
use
http://myhost/myapp/api/getFpmlApi/Fpml.xml?TradeId=234234

Chrome App: launch link in Chrome rather than Safari

I'm developing a Chrome (packaged) app which maintains a set of bookmarks. This opens in its own small window. Clicking on a bookmark opens it in a browser using a link with target set to '_blank'.
On Mac OS X, these open in Safari. Is there anyway of having them open in Chrome?
When you click on a link with target="_blank" in a packaged app, Chrome respects your choice of the default browser and opens the link externally in whatever it is, not necessarily Chrome. In your case, the default system browser must be Safari.
The easy way to open such links in Chrome would be to make it the default browser instead.
If you don't want to do that, but still for some reason insist that your links open in new tabs specifically in Chrome, here is one (perhaps the only) way to achieve that:
Write a companion extension to your app and have your users install it.
In the app, attach an onclick handler to every link, and use chrome.runtime.sendMessage() to send a request to the extension to open the link's URL (in order to do that, you will have to find out your extension's ID and bake it into its manifest, as described here: http://developer.chrome.com/apps/manifest/key.html):
var link = ...;
link.addEventListener('click', function(e) {
e.stopPropagation();
e.preventDefault();
chrome.runtime.sendMessage(
yourExtensionId, { url: link.href }, function(response) {}
);
};
In the extension, define a chrome.runtime.onMessageExternal(data) handler (it will intercept sendMessage() requests from the app), and use chrome.tabs.create() in there to open a new tab:
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
// Don't forget to make sure that |sender| is you app here.
chrome.tabs.create({ url: request.url }, function() {
// If you need to notify the app when the tab opens:
sendResponse(true);
});
// 'true' means that your response is sent asynchronously.
return true;
}
);

How to return pdf document from controller with ajax call in MVC app

I have an #Html.Action link that currently works for returning the user a pdf document from the controller. I want to be able to use an ajax call to perform the same function but I'm stuck on how or even if this can be done. I have tried several different iterations but I never get the pdf to download from the browser window. Here is what I have so far:
$('#Button1').click(function () {
var poNum = "51970";
$.ajax({
type: "GET",
data: "id= " + poNum,
url: '#Url.Action("PoReport", new { controller = "LogisticsTools"})'
});
});
I can copy the Request URL from the Headers window in Chrome Dev Tools and paste it into a new page and the pdf is downloaded. I can see the pdf code in the Response window also but it just doesn't get downloaded when the button is clicked. What am I missing?
You don't want to ajaxify this. Put the URL in an anchor tag and let the browser do the rest. If the browser doesn't recognise the document type or it is configured to force file downloads, the file will download as you expect (i.e. the user will see the download dialog). If the document is recognised, can be opened in the browser, and the browser is configured to open files, the file will open in the browser.
<a href='#Url.Action("PoReport", new { controller = "LogisticsTools"})'
title="Click to Download/Open">
Download
</a>
You can't download PDF file while using the ajax request to server. So instead of that you should use html actionlink. for example
#Html.ActionLink("Convert Data into PDF", "PDFView", null, new { #class= "btn btn-info" });
Above code will generate a link button and you can access Action Method of controller. Also you can use any technique to return PDF file from controller for example you can return PDF file using FileResult class.

how to close IE9 browser session that has a popup message "Do you want to open or save file" displayed

on my web application, after clicking a download button, a popup message with content "Do you want to open or save "abc.txt" from this site?" with 3 buttons ("Open", "Save" & "Cancel") will be displayed at the bottom of page.
I'm trying to close/quit this browser session with below codes:
#driver.execute_script "window.onbeforeunload = function(e){};"
#driver.quit
However, the browser is NOT (but should be) closed. I'm working with Selenium Ruby Webdriver. Please guide me a way to resolve this problem. Thanks so much.
Note that with the above codes, I'm able to close IE9 browser that has the popup message "This page is asking you to confirm that you want to leave - data you have entered many not be saved" with "Leave Page" and "Stay on Page" buttons successfully. But, codes do NOT work in case the popup message with content "Do you want to open or save "abc.txt" from this site?" with 3 buttons ("Open", "Save" & "Cancel") displayed.
WebDriver has no control over these types of "Save file" dialog prompts.
Please peruse this article on this subject (note, his examples are in Java, but they can all be ported easily and with less code in Ruby). http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/
Thus, I would recommend not even clicking the link in question and trying to deal with the dialog. Instead, grab the href value and initiate a Net::HTTP request to it like this SO response shows you how to do: https://stackoverflow.com/a/4581116/1221475 . You can then check the file for correct contents and such using standard Ruby and file parsers.
This is a hack by Dave Haefner. It is written for Java/Selenium combination, but you can easily convert it to Ruby syntax.
If you don't care if a file was downloaded or not and you want to confirm only that a file can be downloaded, you can use an HTTP request. Instead of downloading the file you'll receive the header information for the file which contains things like the content type and length. With this information, you can confirm the file is you expect.
String link = driver.findElement(By.cssSelector("download-link-element")).getAttribute("href");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpHead request = new HttpHead(link);
HttpResponse response = httpClient.execute(request);
String contentType = response.getFirstHeader("Content-Type").getValue();
int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
assertThat(contentType, is("application/octet-stream"));
assertThat(contentLength, is(not(0)));

Resources