Friday 26 December 2014

Setup Custom Page for File Upload

First, create a record.

From this point forward, we will refer to it as MYRECORD.

Next, create the page.


Imagine that the deleted text is MYRECORD.

Next, check that the following delivered codes exist in FILE_ATTACH_WRK Record PeopleCode.
These are all delivered code.

Function display_attachment_buttons(&ATTACHUSERFILE);
   If (&ATTACHUSERFILE <> "") Then
      Hide(FILE_ATTACH_WRK.ATTACHADD);
      UnHide(FILE_ATTACH_WRK.ATTACHVIEW);
      UnHide(FILE_ATTACH_WRK.ATTACHDELETE);
      UnHide(FILE_ATTACH_WRK.ATTACHDET);
   Else
      UnHide(FILE_ATTACH_WRK.ATTACHADD);
      Hide(FILE_ATTACH_WRK.ATTACHVIEW);
      Hide(FILE_ATTACH_WRK.ATTACHDELETE);
      Hide(FILE_ATTACH_WRK.ATTACHDET);
   End-If;
End-Function;

Function delete_attachment(&URL_ID, &ATTACHSYSFILENAME, &ATTACHUSERFILE, &MESSAGE_LVL, &RETCODE)
   
   /* &MESSAGE_LVL: 0 - suppress all messages including errors
                    1 - display all messages
                    2 - suppress only successful message but will display error messages. 
      &RETCODE is the return code from the attachment call.  You can evaluate &retcode and manipulate the attachment buttons as needed in your application.  For example, you may want to call the function display_attachment_buttons(&ATTACHUSERFILE) in ATTACHADD RowInit if addattachment was successful. 
 */
   
   &RETCODE = DeleteAttachment(&URL_ID, &ATTACHSYSFILENAME);
   
   If (&RETCODE = %Attachment_Success) Then
      If &MESSAGE_LVL = 1 Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 10, "DeleteAttachment succeeded");
      End-If;
      &ATTACHUSERFILE = "";
      &ATTACHSYSFILENAME = "";
   End-If;
   
   If (&RETCODE = %Attachment_Cancelled) Then
      If &MESSAGE_LVL = 1 Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 3, "AddAttachment cancelled");
      End-If;
   End-If;
   
   If &MESSAGE_LVL = 1 Or
         &MESSAGE_LVL = 2 Then
      If (&RETCODE = %Attachment_Failed) Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 11, "DeleteAttachment failed");
      End-If;
      If (&RETCODE = %Attachment_FileTransferFailed) Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 13, "DeleteAttachment failed: File Transfer did not succeed");
      End-If;
      /* following error messsage only in iclient mode */
      If (&RETCODE = %Attachment_NoDiskSpaceAppServ) Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 14, "DeleteAttachment failed: No disk space on the app server");
      End-If;
      /* following error messsage only in iclient mode */
      If (&RETCODE = %Attachment_NoDiskSpaceWebServ) Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 15, "DeleteAttachment failed: No disk space on the web server");
      End-If;
      If (&RETCODE = %Attachment_FileExceedsMaxSize) Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 16, "DeleteAttachment failed: File exceeds the max size");
      End-If;
      If (&RETCODE = %Attachment_DestSystNotFound) Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 17, "DeleteAttachment failed: Cannot locate destination system for ftp");
      End-If;
      If (&RETCODE = %Attachment_DestSysFailedLogin) Then
         MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 18, "DeleteAttachment failed: Unable to login into destination system for ftp");
      End-If;
   End-If;
End-Function;

Declare Function IsLegalAttachmentType PeopleCode FILE_ATTACH_WRK.ATTACHUTIL FieldChange;

Function view_attachment(&URL_ID, &ATTACHSYSFILENAME, &ATTACHUSERFILE, &MESSAGE_LVL, &RETCODE)
   
   /* &MESSAGE_LVL: 0 - suppress all messages including errors
                    1 - display all messages
                    2 - suppress only successful message but will display error messages.
      &RETCODE is the return code from the attachment call.  You can evaluate &retcode and manipulate the attachment buttons as needed in your application.  For example, you may want to call the function display_attachment_buttons(&ATTACHUSERFILE) in ATTACHADD RowInit if addattachment was successful.

 IsLegalAttachmentType(&ATTACHSYSFILENAME) condition as of now, will prevent files with a .JSP extension
  from being viewed.  Additional extensions may be added to prevent the viewing of additional file types.
  Please see IsLegalAttachmentType() on FILE_ATTACH_WRK.ATTACHUTIL FieldChange to add additional extensions.
  */
   
   
   /* ICE:1187171004 */
   /*If (Upper(Right(&ATTACHSYSFILENAME, 4)) <> ".JSP") Then*/
   If (IsLegalAttachmentType(&ATTACHSYSFILENAME)) Then
      
      &RETCODE = ViewAttachment(&URL_ID, &ATTACHSYSFILENAME, &ATTACHUSERFILE);
      
      If (&RETCODE = %Attachment_Cancelled) Then
         If &MESSAGE_LVL = 1 Then
            MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 3, "AddAttachment cancelled");
         End-If;
      End-If;
      If &MESSAGE_LVL = 1 Or
            &MESSAGE_LVL = 2 Then
         If (&RETCODE = %Attachment_Failed) Then
            MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 19, "ViewAttachment failed");
         End-If;
         If (&RETCODE = %Attachment_FileTransferFailed) Then
            MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 21, "ViewAttachment failed: File Transfer did not succeed");
         End-If;
         /* following error messsage only in iclient mode */
         If (&RETCODE = %Attachment_NoDiskSpaceAppServ) Then
            MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 22, "ViewAttachment failed: No disk space on the app server");
         End-If;
         /* following error messsage only in iclient mode */
         If (&RETCODE = %Attachment_NoDiskSpaceWebServ) Then
            MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 23, "ViewAttachment failed: No disk space on the web server");
         End-If;
         If (&RETCODE = %Attachment_FileExceedsMaxSize) Then
            MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 24, "ViewAttachment failed: File exceeds the max size");
         End-If;
         If (&RETCODE = %Attachment_DestSystNotFound) Then
            MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 25, "ViewAttachment failed: Cannot locate destination system for ftp");
         End-If;
         If (&RETCODE = %Attachment_DestSysFailedLogin) Then
            MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 137, 26, "ViewAttachment failed: Unable to login into destination system for ftp");
         End-If;
      End-If;
   Else
      /* File extension check failed - reject viewing */
      MessageBox(0, MsgGetText(137, 1, "File Attachment Status"), 0, 0, "ViewAttachment failed: Unauthorized file extension");
   End-If; /* End file extension check */
End-Function;

Go online and set up a URL.
The record that will store the file will be referred to as MYRECORD_FILE.
Root > PeopleTools > Utilities > Administration > URLs

Next, open Component PeopleCode and place the following code in its respective location.

[MYCOMPONENT.GBL.FILE_ATTACH_WRK.ATTACHADD.FieldChange]

Declare Function add_attachment PeopleCode FILE_ATTACH_WRK.ATTACHADD FieldChange;
Declare Function display_attachment_buttons PeopleCode FILE_ATTACH_WRK.ATTACHADD RowInit;

Local number &CurRow, &RETCODE;
Local string &Guid, &URL_ID;

&URL_ID = "URL.MYURL";
&Guid = UuidGenBase64();
add_attachment(@&URL_ID, "", &Guid, 0, True, "Record.MYRECORD", MYRECORD.ATTACHSYSFILENAME, MYRECORD.ATTACHUSERFILE, 2, &RETCODE);

If &RETCODE = %Attachment_Success Then
   display_attachment_buttons(MYRECORD.ATTACHUSERFILE);
   DoSaveNow();
End-If;

[MYCOMPONENT.GBL.FILE_ATTACH_WRK.ATTACHDELETE.FieldChange]

Declare Function delete_attachment PeopleCode FILE_ATTACH_WRK.ATTACHDELETE FieldChange;
Declare Function display_attachment_buttons PeopleCode FILE_ATTACH_WRK.ATTACHADD RowInit;

Local number &Ret;
Local string &URL_ID;

&Ret = MessageBox(%MsgStyle_YesNo, "", 0, 0, "Are you sure you would like to delete the attachment?");
If &Ret = %MsgResult_Yes Then
   &URL_ID = "URL.MYURL";
   delete_attachment(@&URL_ID, MYRECORD.ATTACHSYSFILENAME, MYRECORD.ATTACHUSERFILE, 2, &RETCODE);
   If &RETCODE = %Attachment_Success Then
      display_attachment_buttons(MYRECORD.ATTACHUSERFILE);
      DoSaveNow();
   End-If;
End-If;

[MYCOMPONENT.GBL.FILE_ATTACH_WRK.ATTACHVIEW.FieldChange]

Declare Function view_attachment PeopleCode FILE_ATTACH_WRK.ATTACHVIEW FieldChange;
Declare Function display_attachment_buttons PeopleCode FILE_ATTACH_WRK.ATTACHADD RowInit;

Local string &URL_ID;

&URL_ID = "URL.MYURL";
view_attachment(@&URL_ID, MYRECORD.ATTACHSYSFILENAME, MYRECORD.ATTACHUSERFILE, 2, &RETCODE);

If &RETCODE = %Attachment_Success Then
   display_attachment_buttons(MYRECORD.ATTACHUSERFILE);
   DoSaveNow();
End-If;

[MYCOMPONENT.GBL.MYRECORD.RowInit]

Declare Function display_attachment_buttons PeopleCode FILE_ATTACH_WRK.ATTACHADD RowInit;

display_attachment_buttons(MYRECORD.ATTACHUSERFILE);
MYRECORD.OPRID.Value = PRCSRUNCNTL.OPRID.Value;
MYRECORD.RUN_CNTL_ID.Value = PRCSRUNCNTL.RUN_CNTL_ID.Value;

Now if you have a custom component, set MYRECORD as the search record.

Result:

And that's it.
You have created your very own file upload page.

4 comments:

  1. Just stumbled upon this and have questions.
    Disclaimer: I know this is a few years old and tools have changed which is why considerations in the context of today's tools (8.56/8.56) would not be included.

    What happens when you delete the row using the - button.

    Is the attachment orphaned needing subsequent cleanup?

    Why is this tied to a run control page?

    Thanks for sharing.

    Tom

    ReplyDelete
  2. Really I enjoy your site with effective and useful information. It is included very nice post with a lot of our resources.thanks for share. i enjoy this post. fire sharing

    ReplyDelete
  3. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. video upload

    ReplyDelete