This javascript is nice that it doesn't open a new window or tab.
window.location.assign(url);
Answer from mozgras on Stack OverflowThis javascript is nice that it doesn't open a new window or tab.
window.location.assign(url);
7 years have passed and I don't know whether it works for IE6 or not, but this prompts OpenFileDialog in FF and Chrome.
var file_path = 'host/path/file.ext';
var a = document.createElement('A');
a.href = file_path;
a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
Use window.open("file.pdf", "_blank"). This will open up a new tab temporarily to download the file then close it leaving the original page and URL in tact.
<h1 onclick="Delete(event)">Fahith</h1>
<h1 onclick="Delete(event)">One</h1>
<h1 onclick="Delete(event)">Two</h1>
<h1 onclick="Delete(event)">Three</h1>
<h1 onclick="Delete(event)">Four</h1>
<h1 onclick="Delete(event)">Five</h1>
<h1 onclick="Delete(event)">Six</h1>
<h1 onclick="Delete(event)">Seven</h1>
<h1 onclick="Delete(event)">Eight</h1>
<script>
function Delete(event){
event.target.remove()
}
</script>
This javascript is nice that it doesn't open a new window or tab.
window.location.assign(url);
7 years have passed and I don't know whether it works for IE6 or not, but this prompts OpenFileDialog in FF and Chrome.
var file_path = 'host/path/file.ext';
var a = document.createElement('A');
a.href = file_path;
a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
Maybe you could use the default sharepoint popup-method?
<a href="javascript:SP.UI.ModalDialog.OpenPopUpPage('/SiteAssets/reportingPrintBase.html');">Do something</a>
You can set these parameters:
SP.UI.ModalDialog.OpenPopUpPage(url, callback, width, height);
With:
- url: The URL of the page to be shown in the modal dialog.
- callback: The callback function that runs when the modal dialog is closed.
- width: The width of the modal dialog.
- height: The height of the modal dialog.
Is the html set up correctly in IIS? Looks like a mime/ content-type related thingie.. If not.. Have you tried another Browser?
You can achieve this using the download attribute for <a> elements. For example:
<a href="1251354216241621.txt" download="your-foo.txt">Download Your Foo</a>
This attribute indicates that the file should be downloaded (instead of displayed, if applicable) and specifies which filename should be used for the downloaded file.
Instead of using window.open() you could generate an invisible link with the download attribute and .click() it.
var str = "Name, Price\nApple, 2\nOrange, 3";
var uri = 'data:text/csv;charset=utf-8,' + str;
var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = "data.csv";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
Unfortunately this isn't supported in all browsers, but adding it won't make things worse for other browsers: they'll continue to download the files with useless filenames. (This assumes that you're using a MIME type is that their browser attempts to download. If you're trying to let the user download an .html file instead of displaying it, this won't do you any good in unsupported browsers.)
That does not work in latest Chrome, I have modified that and the following code will work fine,
$("#download_1").on('click', function() {
var json_pre = '[{"Id":1,"UserName":"Sam Smith"},{"Id":2,"UserName":"Fred Frankly"},{"Id":1,"UserName":"Zachary Zupers"}]';
var json = $.parseJSON(json_pre);
var csv = JSON2CSV(json);
var downloadLink = document.createElement("a");
var blob = new Blob(["\ufeff", csv]);
var url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = "data.csv";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
});
So when you click on download_1 id button then it will create an invisible download link and click that. I have used another function to prepare js.
The JSON2CSV function is as follows:
function JSON2CSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var line = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
line += array[i][index] + ',';
}
line = line.slice(0, -1);
str += line + '\r\n';
}
return str;
}
Hope it will help others :)
function startDownload(url) {
window.location.href = url;
}
This will start the download in the same page, exactly like when you click a link without any target other than _self.
To force the download of a file, make sure you send the right headers with it:
Content-Disposition: attachment; filename="mypdf.pdf";
This will make sure that the file is not displayed in the browser instead of being downloaded. Replace the filename part with the filename you want as default on the save as dialog.
window.open will open a new window \ tab (depending on user preferences) ... to just download the file use
window.location.href = url;
You can use this if the url returns a downloadable file rather than a web page
Hello Jaspreet. Thanks for getting back to me. It looks like this occurs mainly with .exe type files and apps. I just bought a new HP laptop with Windows 11. I went to the Microsoft website and downloaded Microsoft office (Excel, Word and PowerPoint) that I purchased. Then, the downloaded files appeared in the upper right corner of the screen after the download completed. When I clicked on the file or on the open file command, nothing happened. What I had to do was go into Microsoft Store, search for the website, go to the website and find the download I wanted. When I did this, it worked. Also, I wanted to download an app. for my printer. The same thing happened. In both cases I had to go through the Microsoft store, find the app. or .exe file I wanted, then download it. With Windows 10, I never had to go through Microsoft Store to download these types of files. Is there some setting I can change, so I can skip the step of going through Microsoft Store.
Go to settings/ apps/ advance app settings
In the first option that says "choose where to get apps" ensure everywhere is selected.