Skip to Content
0
Former Member
Sep 14, 2005 at 09:12 AM

Forcing PDF Download/Stream Error

206 Views

Hello everyone,

I am currently writing an iView that forces the download

of a PDF file instead of opening it by default in the

users browser. I set content type and content disposition

and then try and stream the pdf. It prompts me for download,

as it should, and i can save the file. Once it's downloaded though, it can't open the file in the adobe

Reader and says the file is damaged. I tried opening

it in notepad and the contents of the file were as

follows:

"Error Streaming the Data"

I have included the two pieces of code i have tried below

anyone got any clues as to why it isn't working?

	public void doOnNodeReady(IPortalComponentRequest request, IEvent event)
	{		
		IPortalComponentProfile userProfile = request.getComponentContext().getProfile();
		HttpServletResponse response = request.getServletResponse(true);
			  
		//Retrieve values from properties file
		docURL = userProfile.getProperty("docURL");
		
		file = request.getParameter("rid");

		//set the content type(can be excel/word/powerpoint etc..)
		response.setContentType ("application/pdf");
		
		//get the file name
		String name = file.substring(file.lastIndexOf("/") + 1,file.length());
		
		//set the header and also the Name by which user will be prompted to save
		response.setHeader ("Content-Disposition", "attachment; filename="" + name + """);
	
	
		String ErrorStr = null;
		try{
			  //find the right MIME type and set it as contenttype
			  PrintWriter outstr = new PrintWriter(response.getOutputStream());
			  InputStream in = null;       
			   try{
				   URL url	= new URL(URLEncoder.encode("http://mydomain.goes.here" + file));
				   URLConnection urlc= url.openConnection();
				   int length = urlc.getContentLength();
				   in = urlc.getInputStream();  
				   response.setContentLength(length);
				   int ch;
				   while ( (ch = in.read()) != -1 ) {
					outstr.print( (char)ch );
				   }
				} catch (Exception e) {
						e.printStackTrace();
						ErrorStr = "Error Streaming the Data";
						outstr.print(ErrorStr);
				} finally {
						if( in != null ) {
						  in.close();
						}
						if( outstr != null ) {
						  outstr.flush();
						  outstr.close();
						}
				}  
			  }
			  catch(Exception e){
					  e.printStackTrace();
			  }

	
		}

	public void doOnNodeReady(IPortalComponentRequest request, IEvent event)
	{		
		IPortalComponentProfile userProfile = request.getComponentContext().getProfile();
		HttpServletResponse response = request.getServletResponse(true);
			  
		//Retrieve values from properties file
		docURL = userProfile.getProperty("docURL");
		
		file = request.getParameter("rid");
		
		
		//set the content type(can be excel/word/powerpoint etc..)
		response.setContentType ("application/pdf");
		
		//get the file name
		String name = file.substring(file.lastIndexOf("/") + 1,file.length());
		
		//set the header and also the Name by which user will be prompted to save
		response.setHeader ("Content-Disposition", "attachment; filename="" + name + """);

		//OPen an input stream to the file and post the file contents thru the 
		//servlet output stream to the client m/c
		
		int bit = 256;
		int i = 0;

		try
		{
			URL u = new URL("http://tvms0019.ad.bgep.co.uk" + file);
			InputStream in = new FileInputStream(u.getFile());
			ServletOutputStream outs = response.getOutputStream();
			
			while ((bit) >= 0)
			{
				bit = in.read();
				outs.write(bit);
			}
		
			outs.flush();
			outs.close();
			in.close();	
		}
		catch (Exception ioe)
		{
			ioe.printStackTrace(System.out);
		}
	}

Note that i have tried both these pieces of code using

URL and File and neither work. I've tried printing

some debugging messages and the file path is correct.

Thanks,

Alessandro