Skip to main content
12-Amethyst
February 28, 2023
Solved

Best way to download files from Windchill Server to client

  • February 28, 2023
  • 1 reply
  • 3872 views

I'm looking to download a zip from my windchill server(temp location) to the client machine.

The zip was actually created as a result of an custom action.

So basically, when I click on the action a zip should get downloaded, that's my requirement.

Please suggest me your ideas or share me any existing solution.

Many thanks in advance.

 

 

Best answer by HelesicPetr

Hi @MV_10441462 

depends how do you start your function. 

usually you get request and get the outputstream from it and flush (send) the data to the client as @avillanueva's example shows you. . 

 

here is short snip with a zipStream but you need to learn how to add files to the zip stream

 

 

HTTPResponse resp; // you need to get respons from commandBean or whereever HttpServletResponse response = nmCommandBean.getResponse(); 

ByteArrayOutputStream zipOut = new ByteArrayOutputStream();
CheckedOutputStream checksum = new CheckedOutputStream(zipOut, new Adler32());
ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(checksum));

InputStream contentStream = ContentServerHelper.service.findContentStream(appDatatToZip);
		// definice filename s cestou v zip
String fileZipEntry = appDatatToZip.getFileName();
addStreamToZip(fileZipEntry, zipOutputStream, contentStream); // annother sub method where the content is writen to zip

zipOutputStream.close();
		
OutputStream output = resp.getOutputStream();

resp.setHeader("Content-disposition", "attachment; filename=zipDownloadFile.zip");
resp.setHeader("Content-Type", "application/zip");

output.write(zipOut.toByteArray()); // zip stream written to the respons outputStream

output.flush();
output.close();

 

 

 

PetrH

1 reply

avillanueva
23-Emerald I
23-Emerald I
February 28, 2023

I did this with a JSP tied to that custom action:

BOMExportExcelUtility excelUtil=BOMExportExcelUtilityImpl.createBOMExportExcelUtility();
 //7. Sub-module create Excel File for user download
 response.reset();
 excelUtil.createExcelFile(response.getOutputStream(),sheet1,sheet2);
 ServletOutputStream outs = response.getOutputStream();
 response.setHeader("Content-type","application/ms-excel");
 String version=VersionControlHelper.getVersionIdentifier((Versioned)bom).getValue();
 if (version.indexOf('.')!=-1)
 version=version.substring(0, version.indexOf('.')); //just get first part
 
 String filename=bom.getNumber()+version;
 response.setHeader("Content-disposition","attachment; filename="+filename+".XLS");//should prompt for download
 
 excelUtil.writeFile();
 //8. Return response to user's browser session.
 outs.flush();
 outs.close();

This is the section where my code creates an XLS file and streams it back to the client. User downloads the file.

HelesicPetr
22-Sapphire II
22-Sapphire II
March 1, 2023

Hi @MV_10441462 

example  from @avillanueva is very good one how to download file.

I would add that you need to write own sollution. 

 

Create zip in java

Collect files from Windchill  

Add files to the zip

push the zip stream to the client user

 

PetrH

PetrH
12-Amethyst
March 1, 2023

Hi @HelesicPetr 

 

Can you please help me with the snippet to push the zip stream to client user?