• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: PDF and appendToResponse
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: PDF and appendToResponse


  • Subject: Re: PDF and appendToResponse
  • From: Kieran Kelleher <email@hidden>
  • Date: Tue, 27 Feb 2007 12:19:42 -0500

Just return it like a download file with content disposition set to inline or sth like that

Here is a WOComponent I use for generic downloading. For PDF just set the correct mime type and content disposition. The client browser will ultimately decide what to do with it anyway.

HTH, Kieran

//
// DownloadDataPage.java: Class file for WO Component 'DownloadDataPage'
// Project cheetah
//
// Created by kieran on 1/27/06
//

import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;
import er.extensions.*;
import java.io.*;

/** A page WOComponent that generically downloads data (NSData)
to the user in context. */
public class DownloadDataPage extends WKPageComponent {
private static org.apache.log4j.Logger log = WKLoggerFactory.getLogger( DownloadDataPage.class );
protected NSData _dataToDownload;
protected String _downloadFilename;
protected String _stringToDownload;
protected int streamingContentSize = 0;
protected int streamingBufferSize = ERXProperties.intForKeyWithDefault ( "cheetah.streamingDownload.defaultBufferSize", 4096 );

public DownloadDataPage(WOContext context) {
super(context);
}


/** @return the data to download. Defaults to text 'empty file - nothing to download' */
public NSData dataToDownload()
{
if ( _dataToDownload == null ) {


// Check for the string and convert to NSData with default encoding if supplied
if ( stringToDownload() == null ) {
_dataToDownload = new NSData( "empty file - nothing to download",
ERXProperties.stringForKey( "cheetah.defaultTextEncoding" ) );
} else {
_dataToDownload = new NSData( stringToDownload(),
ERXProperties.stringForKey( "cheetah.defaultTextEncoding" ) );
}
}
return _dataToDownload;
}
public void setDataToDownload(NSData newDataToDownload)
{
_dataToDownload = newDataToDownload;
}


    /** @return the filename for the file after it is downloaed.
        Defaults to an arbitrary name. */
    public String downloadFilename()
    {
        if ( _downloadFilename == null ) {
            _downloadFilename = "downloadedfile";
        }
        return _downloadFilename;
    }
    public void setDownloadFilename(String newDownloadFilename)
    {
        _downloadFilename = newDownloadFilename;
    }

    File _fileToDownload;

    /** @return the file to be downloaed (streaming) */
    public File fileToDownload() {
        return _fileToDownload;
    }

    /** Set the file to be downloaded. */
    public void setFileToDownload( File aFile ) {
        _fileToDownload = aFile;
    }

    InputStream _inputStreamToDownload;

/** @return an InputStream containing the data to download.
This is preferred method, especially for dealing with data that may be large
and timeconsuming*/
public InputStream inputStreamToDownload() {
return _inputStreamToDownload;
}


public void setInputStreamToDownload( InputStream inStream, int contentSize ) {
_inputStreamToDownload = inStream;
streamingContentSize = contentSize;
}


public void appendToResponse( WOResponse aResponse, WOContext aContext ) {
super.appendToResponse( aResponse, aContext );


// Set default encoding
aResponse.setContentEncoding( ERXProperties.stringForKey ( "cheetah.defaultTextEncoding" ) );


// We check for the three downloadable types and whichever we encounter first it the one that is
// downloaded
if ( fileToDownload() != null ) {
// Download files as a stream
try {
aResponse.setContentStream(new FileInputStream ( fileToDownload() ) , streamingBufferSize, (int)fileToDownload ().length() );
} catch (IOException ioExc ) {
log.error( "Error setting content stream", ioExc );
}
} else if ( inputStreamToDownload() != null ) {
aResponse.setContentStream( inputStreamToDownload(), streamingBufferSize, streamingContentSize );
} else if ( stringToDownload() != null ) {
// Download string
aResponse.setContent( stringToDownload() );
} else if ( dataToDownload() != null ) {
// Download NSData
aResponse.setContent( dataToDownload() );
}


// Set content headers
aResponse.setHeader( contentType(), "content-type" );
aResponse.setHeader( contentDisposition(), "content- disposition");


    }


protected String _contentDisposition;

/** @return content-disposition header. Defaults to <code>"attachment;filename=\"" + downloadFilename() + "\""</code>*/
public String contentDisposition() {
if ( _contentDisposition == null ) {
_contentDisposition = "attachment;filename=\"" + downloadFilename() + "\"";
}
return _contentDisposition;
}


    public void setContentDisposition( String contentDisposition ) {
        _contentDisposition = contentDisposition;
    }

    protected String _contentType;

/** @return content-type header. Defaults to <code>"application/ octet-stream"</code> */
public String contentType() {
if ( _contentType == null ) {
_contentType = "application/octet-stream";
}
return _contentType;
}


    public void setContentType( String contentType ) {
        _contentType = contentType;
    }

    public String stringToDownload()
    {
        return _stringToDownload;
    }
    public void setStringToDownload(String newStringToDownload)
    {
        _stringToDownload = newStringToDownload;
    }

}




_______________________________________________ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >PDF and appendToResponse (From: Gino Pacitti <email@hidden>)

  • Prev by Date: Re: PDF and appendToResponse
  • Next by Date: Re: Problem Connecting to OpenBase
  • Previous by thread: Re: PDF and appendToResponse
  • Next by thread: Need advice on using image buttons for form submission
  • Index(es):
    • Date
    • Thread