html2canvas in angular 4

醉酒当歌 提交于 2019-12-03 13:27:50

问题


I am able to take a screenshot using html2canvas in angular 4 but i need to send the string image to the server side using a http post call

Component

import { Component, OnInit, NgZone } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { EventsEmitter } from '../../assets/scripts/services/eventsEmitter';
import { WindowRef } from '../../assets/scripts/services/window';
import { ImagesService } from '../images/images.component.service';
import { DomSanitizer } from '@angular/platform-browser';
import * as html2canvas from "html2canvas";


@Component({
    selector: 'categories',
    templateUrl: 'app/components/view/view.component.html',
    styleUrls: ['app/components/view/view.component.css'],
    providers: [ImagesService]
})
export class ViewComponent {
    
   

    constructor(
        private route: ActivatedRoute,
        private router: Router,
        private imagesService: ImagesService,
        private eventsEmitter: EventsEmitter
        private sanitizer: DomSanitizer,
        private window: WindowRef) {
        this.window.nativeWindow.scrollTo(0, 0);
    }

    ngOnInit() {
    }

    pdfDownload() {
        html2canvas(document.body).then(function (canvas) {
            var imgData = canvas.toDataURL("image/png");
            document.body.appendChild(canvas);
        });
    }


    AddImagesResource(query: any) {
        this.imagesService.addCanvasResource(query)
            .subscribe(response => {
                this.eventsEmitter.broadcast('Success', 'Changes Saved Succesfully');
            },
            error => {
                this.eventsEmitter.broadcast('Error', 'Error Occured');
            });
    }
}
<a data-html2canvas-ignore (click)="pdfDownload()">screenshot</a>

Service that i am calling to do a post

 addCanvasResource(body: Object): Observable<any> {
        let bodyString = JSON.stringify(body);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        return this.http.post(this.baseUrl + 'api/v3/images/AddCanvasImage', body, options)
            .map((response: Response) => {
                if (response.status < 200 || response.status >= 300) {
                    throw new Error('This request has failed ' + response.status);
                }
                else {
                    return response;
                }
            });
    }  

I am unable to access AddImagesResource() function in html2canvas

can you please tell me how to achieve the above functionality


回答1:


pdfDownload() {
    let self = this;//use this variable to access your class members inside then().
    html2canvas(document.body).then(canvas => {
        var imgData = canvas.toDataURL("image/png");
        self.AddImagesResource(imgData);
        document.body.appendChild(canvas);
   });
}



回答2:


When providing the callback for a promise in Angular, you should use an arrow function, rather than an anonymous function. Arrow functions bind to the current context correctly, so the function you are trying to call will be accessible.

Try this instead:

pdfDownload() {
    html2canvas(document.body).then(canvas => {
        var imgData = canvas.toDataURL("image/png");
        this.AddImagesResource(imgData);
        document.body.appendChild(canvas);
    });
}



回答3:


Make sure to import the script under the scripts list in angular-cli.json

"scripts": [
"../node_modules/html2canvas/dist/html2canvas.min.js"
]

And in the class as:

import * as html2canvas from "html2canvas"


来源:https://stackoverflow.com/questions/44088988/html2canvas-in-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!