angular 2 经过http get 下载excel 内容错误

服务端代码:javascript

public ResponseEntity<Resource> exportsequence(@PathVariable String modelname) throws IOException {
        XSSFWorkbook  wb = constructExportData(modelname);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        InputStreamResource resource = new InputStreamResource(os.toInputStream());
        os.close();
        return ResponseEntity.ok().contentType(MediaType.valueOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")).body(resource);
    }
    /**
     *
     *
     * @param modelName
     */
    private XSSFWorkbook constructExportData(String modelName) {
        XSSFWorkbook  wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(ExcelFileHandlerUtil.SEQUENCE_SHEET_FC);
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue(ExcelFileHandlerUtil.MODEL_STRING);



        return wb;
    }

 

客户端代码:java

return this.http.get(`${this.resourceUrl}/exportsequence/${modelName}`);

用这个下载的时候二进制内容都是正确的,但是打开文件老报错,打不开。app

原来要在请求上加一个   responseTypethis

修改代码以下:code

const requestOptions = {
    responseType: ResponseContentType.Blob
};

return this.http.get(`${this.resourceUrl}/exportsequence/${modelName}`, requestOptions);

记得加:orm

import {Http, Response,ResponseContentType } from '@angular/http';
相关文章
相关标签/搜索