I am opening a new tab to display a PDF file. I have the PDF file data on a bytearray, base 64.
I am able to get the data, and display it doing this:
downloadFile(strData, name) {
var newdata = "data:" + "application/pdf" + ";base64," + (strData);
var newWindow = window.open(newdata, "_blank");
newWindow.onload = function() { document.title = "My title"; }
return true;
}
The problem i am having is that i am not able to set the title to the new tab opened.
I would like to set a title like "PDF File" or just the name of the document (i am getting the file data and the file name separately and passing it to my downloadFile function.
Is there any way to set the title to this tab? Thanks in advance!
Try this:
....
var newWindow = window.open(newdata, "_blank");
newWindow.document.title = "Some title";
....
EDIT:
Another way to do this might be to send an iframe to a new window instead of opening it directly with the base64 string.
So something like:
var newWindow = window.open();
newWindow.document.write('<iframe src="data:application/pdf;base64,' + (strData) + '" frameborder="0" allowfullscreen></iframe>');
newWindow.document.title = "Your Title Here";
This worked for me.
var newWindow = window.open(newdata, "_blank");
setTimeout(function(){ newWindow.document.title = 'my new title'; }, 1000);
Related
I have a trigger set up in Google Sheets so a URL is automatically opened in a new browser window. This works if the URL is hardcoded. I want the URL to be a variable. How do I pass the URL variable from Apps Script to HTML script? I'm a novice coder so please explain like I'm 5.
This function works if the URL is text like 'https://www.google.com'
function openMap() {
var maplink = SpreadsheetApp.getActiveSheet().getRange("B2").getValues();
var js = "<script>window.open('https://www.google.com', '_blank', 'width=800, height=600');google.script.host.close();</script>";
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
}
This function does not if the URL is a variable (I checked the maplink value and its a valid URL)
function openMap() {
var maplink = SpreadsheetApp.getActiveSheet().getRange("B2").getValues();
var js = "<script>window.open('maplink', '_blank', 'width=800, height=600');google.script.host.close();</script>";
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
}
You can join strings in the following manner:
...
var url = 'https://www.google.com';
var js = "<script>window.open('" + url + "', '_blank', 'width=800, height=600');google.script.host.close();</script>";
...
Basically
you close the first part of your hardcoded string by closing the quotes
add the dynamical variabl with +
continue the hardcoded string by appending the second part with + and opening the quotes again
I hope this is clear!
I am trying to download multiple files at once. At first I use window.location = url but now it doesn't seem to work.
I've changed it to window.open(url, "_blank") and it only downloads the first one:
window.open("/host/Controller/DownloadDasFiles?paramId=204");
window.open("/host/Controller/DownloadDasFiles?paramId=205");
window.open("/host/Controller/DownloadDasFiles?paramId=206");
public FileResult DownloadDasFiles(int paramId)
{
var dasControl = UnityConfig.container.Resolve<IDasControlService>();
var filename = dasControl.GetDasFileToDownload(paramId);
return File(filename, "application/octet-stream", Path.GetFileName(filename));
}
In my real case I do this after AJAX success in a javascript loop, but this code should work, shouldn't it?
This may help. It will open all 3 windows at same time.
let test = [204, 205, 206];
let downLoadFileBaseUrl = '/host/Controller/DownloadDasFiles?paramId='
test.forEach(element => {
let file = `${downLoadFileBaseUrl}+${element}`;
window.open(file, '_blank', 'File :' + element + ',scrollbars=1,menubar=0,resizable=1,width=850,height=500');
});
This is my try, were it opened on newtab, But its always showing test pdf as title
function titlepath(path,name){
alert(path);
alert(name);
document.title = name;
window.open(path, '_blank');
}
This solution works for me.
Question: How to change the title of pdf in newly opened tab
function titlepath(path,name){
//In this path defined as your pdf url and name (your pdf name)
var prntWin = window.open();
prntWin.document.write("<html><head><title>"+name+"</title></head><body>"
+ '<embed width="100%" height="100%" name="plugin" src="'+ path+ '" '
+ 'type="application/pdf" internalinstanceid="21"></body></html>');
prntWin.document.close();
}
I am trying to download JSON data into a JSON file using below mentioned code but the code just gives me a blank Internet Explorer. I need code to download JSON file without having any event raised at user interface.
var dataUri = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(JSONData));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataUri);
downloadAnchorNode.setAttribute("download", "CarData.json");
downloadAnchorNode.click();
You are getting a blank page because you aren't inserting the anchor node into the DOM. Consider the following snippet. Even if you remove the download attribute, you will be taken to a page with JSON data on it.
var JSONData = {
foo: "bar"
};
var dataUri = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(JSONData));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.innerHTML = "Click";
downloadAnchorNode.setAttribute("href", dataUri);
downloadAnchorNode.setAttribute("download", "CarData.json");
document.body.appendChild(downloadAnchorNode);
downloadAnchorNode.click();
Here it is without the download:
var JSONData = {
foo: "bar"
};
var dataUri = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(JSONData));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.innerHTML = "Click";
downloadAnchorNode.setAttribute("href", dataUri);
document.body.appendChild(downloadAnchorNode);
downloadAnchorNode.click();
Additionally, as others have pointed out, IE does not support the download attribute.
I am trying to implement pdf preview functionality.
This is my angular js code:
//preview document
FileUploader.prototype.preview = function (file) {
var ctrl = this;
var pdfFile = file;
var file = new Blob([(pdfFile)], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
console.log(fileURL);
ctrl.content = ctrl.services.$sce.trustAsResourceUrl(fileURL);
var myWindow = ctrl.services.$window.open(ctrl.content);
myWindow.document.title = "custom file name";
}
I am able to preview the document but I am unable to give a custom title to the $window.open
I think the unique identifier is set automatically if the title is empty in the document properties.
How can I override the title?