pdf.js實現文件的預覽(同時還附帶下載,本地pdf預覽等功能)

下載

PDF.js官網下載項目

代碼修改

在web目錄下新建一個index.html文件

<body>
    <button class="download">下載pdf</button>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        $(function(){
            $('.download').on('click',function(){
                axios.interceptors.request.use(function (config) {
                    // config.headers['Authorization'] = 'Bearer d76e0e17-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
                    return config;
                }, function (error) {
                    return Promise.reject(error);
                });

                axios.get('http://xxx.xxx.xxx.xxx:xxxx/generate/ReadPDF',{
                    responseType: 'blob',
                })
                .then(function (response) {
                    let url = window.URL.createObjectURL(new Blob([response.data], {type:"application/pdf"}));
                    window.open('viewer.html?file=' + encodeURIComponent(url))
                })
                .catch(function (error) {
                    console.log(error);
                })
                
            })
        })
    </script>
</body>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章