PeopleCode | Opening a file and displaying its contents to the user
A. Options
Viewing a file attachment is PeopleCode is done via the use of the ‘ViewAttachment’ function. Similar to ‘AddAttachment’, there is also a second version of the view file functionality available in the FILE_ATTACH_WRK function library. An example of each method will be included below, in addition to a related function (‘ViewContentURL’).
The examples assume that the file you wish to view is currently stored on the file server. Although you can use ‘ViewAttachment’ to directly view this file, in practice it is much simpler to view a file that is stored in a database file record. Therefore, with this intention in mind, the code below first copies the file to the database via the ‘PutAttachment’ function. Then, the code calls ‘ViewAttachment’ directly on the database record. Finally, the code removes the file from the database via ‘DeleteAttachment’.
This may seem a roundabout way of displaying a file, but it removes a lot of the complexity surrounding the ‘URL ID’ field, the first parameter of the attachment functions. Specifying this first parameter is overly complicated for a file stored on the server, as it depends on the underlying operating system. On the other hand, the approach described below works independently of the operating system.
B. Native ViewAttachment
The code assumes that both the ‘&file_name’ and ‘&file_dir’ variables have already been set prior to calling the code, such as in the following example:
&file_name = "sample.pdf"; &file_dir = "/u02/app/psft/ps_cfg/appserv/PSDEV/files/sample.pdf";
Note that the &file_dir variable must include the full file path including the file name. Also note that there is no ‘%FilePath_Relative’ or ‘%FilePath_Absolute’ option here.
/* Declare variables */ Local number &retcode; Local string &file_name, &file_dir; Local string &URL_ID = "record://XX_FILE_TMP"; /* Copy the attachment to the database record */ /* &file_dir must include the full directory and file name */ &retcode = PutAttachment(&URL_ID, &file_name, &file_dir); /* If successful, continue with view attachment */ If &retcode = 0 Then &retcode = ViewAttachment(&URL_ID, &file_name, &file_name); End-If; /* If successful, delete the attachment from the database record */ If &retcode = 0 Then &retcode = DeleteAttachment(&URL_ID, &file_name); End-If; /* If any non-zero return code was encountered, display an error */ If &retcode <> 0 Then Error MsgGet(99999, 999, "Message not Available"); End-If;
C. Enhanced view_attachment
Here’s the equivalent code using the enhanced ‘view_attachment’ function from FILE_ATTACH_WRK:
Declare Function view_attachment PeopleCode FILE_ATTACH_WRK.ATTACHVIEW FieldChange; /* Define file record and file name */ Local string &URL_ID = "record://XX_FILE_TMP"; Local string &file_name = "sample.pdf"; view_attachment(&URL_ID, &file_name, &file_name, 0, &return_code);
D. ViewContentURL
Finally, as an alternative to ‘ViewAttachment’, you can use the ‘ViewContentURL’ function. This is handy for displaying the content of external websites, or for accessing files that are permanently stored on the Internet, complete with their own URL:
ViewContentURL("http://www.google.com");
The ‘ViewContentURL’ function automatically opens a new window to display the content.
See Also:
Tip 035: Uploading a File Attachment