Skip to main content
Participating Frequently
March 26, 2018
Answered

Rename an attached file?

  • March 26, 2018
  • 1 reply
  • 1454 views

Hi,

I have a form with multiple javascript buttons for users to upload files.  The issue is that if someone deletes a file, and uploads it again, the order of the file is out of sync and doesnt match the button that was used to upload.

ie. button1 to upload document1, button2 to upload document2, button3 to upload document3, delete document1, reupload document1 using button1

document order is as follows:

document2, document3, document1

Sequence of the documents is important, but if I can't get around that, is there anyway to append a prefix to the filename based on the name of the button that was used to upload the document?

ie. button1 to upload document1, document is named 'button1 - document1', etc.

thoughts?  thanks in advance!

This topic has been closed for replies.
Correct answer Thom Parker

Ok nearly there.  Just can't figure out how to reference the button name in importDataObject..

I'm at the point where any of the upload buttons replaces the previous attachment.  So i'm assumping the importDataObject needs to reference the unique name of each of the buttons so it will only replace the file that was added using that same button.

    try {

        var rc = this.importDataObject("Attachment");

        if (rc) {

            app.alert({

                cMsg: "Attachment successful.\r\rOpen the Attachments panel to access the attached file(s).",

                cTitle: "Attachment Successful",

                nIcon: 3,

                nType: 0

            });

        } else {

            app.alert({

                cMsg: "Attachment cancelled.",

                cTitle: "Attachment Cancelled",

                nIcon: 3,

                nType: 0

            });

        }    

    } catch (e) {

        app.alert({

            cMsg: "Could not attach file.",

            cTitle: "Could not attach file",

            nIcon: 3,

            nType: 0

        });

    }


Use this code

      var rc = this.importDataObject(event.targetName);

1 reply

Thom Parker
Community Expert
Community Expert
March 26, 2018

Yes there is. In JavaScript, file attachments are called "Data Objects".

Many of the Data Object properties can be modified, including the name. You can find out all you need in the Acrobat JS ref

Here's the entry for the Data Object: Acrobat DC SDK Documentation

How you find the specific data object you want to modify depends on how the files are being attached. What exactly is your process. Be very specific.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
March 27, 2018

Awesome, thanks Thom!

Current process is a form with multiple upload buttons throughout the form for the user to upload various supporting documents.  Each button is setup the same way with a default name (button1, button2, etc) and the same javascript currently to upload the document to the attachments section (see below). 

if (app.viewerVersion < 11) {

    import_pre_11()

} else {

    import_11();

}

I'll take a look at the SDK to see if I can figure it out on my own.  My assumption is to give each button a unique name, and to figure out how to append the name of the button as a prefix to the filename that is uploaded by the user.