Interfaz
show(e) { this.axios({ url: this.url.labelPrint, method: 'post', responseType: 'blob', type: 'multipart/form-data', timeout: 150000, data: { //封装要用的参数 sampleNumber: e.reportNumber, sampleName: e.sampleName, sampingDate: e.sampingDate, deliveryDate: e.deliveryDate, deliveryDates: e.deliveryDates, deliveryAddress: e.deliveryAddress, standbywhere: e.standbywhere, samplequantity: e. muestraCantidad, clave: e.id } }).then(res => { const elink = document.createElement('a') elink.style.display = 'none' document.body.appendChild(elink); //El nombre del mismo nombre const fileName = e .reportNumber + e.sampleName+'.zip' elink.download = fileName let binaryData = []; binaryData.push(res); elink.href = window.URL.createObjectURL(new Blob(binaryData)); elink.click() ; URL.revokeObjectURL(elink.href); // Liberar el objeto URL this.$message.success("descargado correctamente"); document.body.removeChild(elink); }) },
fondo java
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
prueba (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
// Genera múltiples archivos
para (int a = shu; a > 0; a--) { // Crea cada archivo docx de etiqueta String fileName = sampleNumber + '-' + a + sampleName + "label.docx"; String filePath = csbcbhUrl.getUrlFile() + fileName; contractTestReportController.parse(templatePath, templateName, filePath, map); // Agregue el archivo de etiquetas al archivo Zip byte[] buffer = new byte[1024]; FileInputStream inputStream = new FileInputStream( filePath) ; zipOutputStream.putNextEntry(nueva ZipEntry(nombre de archivo)); int len; while ((len = inputStream.read(buffer)) != -1) { zipOutputStream.write(buffer, 0, len); } inputStream.close () ;
zipOutputStream.closeEntry();
// Registra el nombre del archivo Zip
objects.add(filePath);
}
// Establece el tipo de respuesta y la información del encabezado del archivo Zip
httpServletResponse.setContentType("application/zip");
httpServletResponse. setHeader("Content -Disposition", "attachment; filename=\"sample_labels.zip\""); }
//
Descarga todos los bytes del archivo Zip al cliente
ServletOutputStream out = httpServletResponse.getOutputStream();
outputStream.writeTo( out);
out .flush();
out.close();
outputStream.close();
// Eliminar archivos generados temporalmente
para (String filePath: objetos) { File file = new File(filePath); file.delete(); }