Skip to main content
Participating Frequently
March 26, 2018
Answered

Rename an attached file?

  • March 26, 2018
  • 1 reply
  • 1444 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
Adobe 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

Possible to not only rename the file that is added based on 'button1.filename', but to also replace a file if the button is used to upload more than one file?  ie. button1 is used to upload a file, button1.filename.  save file, then at a future date upload a revised document using button1 so it replaces button1.filename with button1.filename?

Thom Parker
Adobe Expert
March 27, 2018

I imagine that the script that attaches the file uses the "doc.importDataObject()" function? The first parameter of this function is a name that is used to associate the file. What value is being used for this name?   This is where the Button name can come into play, i.e. use the button name as the name associated with the file. Then the importDataObject function will always replace the same file.  The file name displayed on the attachments panel is the "path" property of the DataObject, not the name. Since the user never actually sees the name value, it can be whatever is necessary to manage the DataObjects.

I make a bit of a mistake in telling you the DataObject properties can be edited. In fact, only the description can be edited. However, since you can control the "name" on the initial import you shouldn't need to edit it later. But if this is really necessary there is a way to get around it, but unfortunately it requires a privileged context for the script. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often