david d
david d

Reputation: 43

how to implement window.open kind of functionality in Angular 4

How do you implement window.open() kind of functionality in Angular 4?

function open(){
    var URL = "file:///C:\data2.pdf";

    window.open(URL, null);
}

I am getting the below error message:

Cannot open local file - Chrome: Not allowed to load local resource.

Let me know if there is any alternative of window.open() in Angular 4.

Upvotes: 2

Views: 25991

Answers (3)

esther44
esther44

Reputation: 63

You should use Router.navigate Angular method:

import { Router } from '@angular/router';
    
constructor(
   private router: Router
) {}
    
open(){
   var URL = "file:///C:\data2.pdf";
   this.router.navigate(URL);
}

Upvotes: 2

You must introduce the file in the assets of the Angular project. To access to the file you introduce 'assets/file2.pdf'.

window.open('assets/file2.pdf');

Don't use an absolute path, because if you deploy in a subdirectory doesn't work.

Upvotes: 2

Abylay
Abylay

Reputation: 344

You should use a web server. Browser not allowed to getting client local file through JavaScript. Example:

var url = '//localhost:3000/data2.pdf';
window.open(url);

Here web server address localhost, and port 3000. As a web server can be for example Node JS, Apache, etc.

Upvotes: 0

Related Questions