簡體   English   中英

導出到 .xls 適用於開發環境但不適用於生產/失敗 - 網絡錯誤

[英]Export to .xls works on dev environment but not on production / Failed - Network Error

我似乎找不到我的代碼問題,它在我的開發環境中運行良好,但在生產環境中卻沒有。 這不是瀏覽器的問題(Firefox、IE 和 Chrome 三個都失敗),也不是文件大小的問題,如此處的其他問題所示。 我認為這可能是 Token 問題,因為我一開始沒有提供,但實施后發現問題仍然存在。

當提示下載時,文件顯示Failed - Network Error

這是一些代碼位,從客戶端開始

generateExcel( ){
        this.detector.markForCheck( );
        this.api.post( 'invoice/excel_liquidation', { liquidation: this.list } ).subscribe( ( response: any ) => {
            if( response.success ) { 
                this.toast.success( 'Excel correctly generated!' );
                let url = response.file.url // + '?token=' + this.auth.jwt;
                this.generateDownloadUrl( url, response.file.name );
                this.detector.markForCheck( );
            }
            this.detector.markForCheck( );
        }, ( error: any ) => {
            this.detector.markForCheck( );
        });
    }

generateDownloadUrl( url: string, name: string ) {
        let a    = document.createElement( 'a' );
        a.href   = url;
        a.download = name;
        document.body.appendChild( a );
        a.click( );

        a.remove( );

    }

最后是服務器端(解析和其他不必要的數據除外):

exportExcel: ( headers, content, file_name ) => {       
        /* SOME ERROR HANDLING */
        if( !file_name ) file_name = 'export-' + app.date.now( 'YYYYMMDDHHmmss' );
        let file = '';      
        headers.map( header => {
            if( typeof header === 'string' && header.toString( ).trim( ) !== '' ) header = header.toString( ).trim( ).replace( /_/g, ' ' ).toUpperCase( );
            file += header + '\t';
        });
        file += '\n';
        content.map( row => {
            headers.map( header => {
                file += row[ header ] + '\t';
            });
            file += '\n';
        });
        let save_file = app.paths.storage + 'temp/' + file_name + '.xls'; 
        return fs.writeFileAsync( save_file, file, 'utf-8' ).then( ( ) => {
            return Promise.resolve( { path: save_file } );
        });
    }, 

任何幫助深表感謝。

像這樣修改之前的方法后問題解決了:

 generateDownloadUrl( url: string, name: string ) {
            let a    = document.createElement( 'a' );
            a.href   = url;
            /*a.download = name;*/
            a.target = '_blank';
            document.body.appendChild( a );
            a.click( );
            a.remove( );

        } 

簡而言之,嘗試在新選項卡中打開 .xls 文件而不是直接下載它作為解決該問題的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM