Rename an attached file?

New Here ,
Mar 26, 2018

Copy link to clipboard

Copied

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!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Use this code

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

TOPICS
Acrobat SDK and JavaScript, Macintosh, Windows

Views

307

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Rename an attached file?

New Here ,
Mar 26, 2018

Copy link to clipboard

Copied

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!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Use this code

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

TOPICS
Acrobat SDK and JavaScript, Macintosh, Windows

Views

308

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 26, 2018 0
Adobe Community Professional ,
Mar 26, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 26, 2018 0
New Here ,
Mar 27, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 27, 2018 0
New Here ,
Mar 27, 2018

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 27, 2018 0
Adobe Community Professional ,
Mar 27, 2018

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 27, 2018 0
New Here ,
Mar 27, 2018

Copy link to clipboard

Copied

Upon further investigation, I discovered that there is document level javascript and not just at the button level.  the person who setup the document used the javascript referenced at this link: Add an "Attach File" button to a PDF form.

so the 'doc.importDataObject()' function is used as:

    try {

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

        if (rc) {

            attachment_num += 1;

            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

            });

        }    

is there a way I can see the name that is associated to the file?  All I see is what is displayed in the Attachments view pane.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 27, 2018 0
New Here ,
Mar 27, 2018

Copy link to clipboard

Copied

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

        });

    }

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 27, 2018 0
Adobe Community Professional ,
Mar 27, 2018

Copy link to clipboard

Copied

Use this code

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 27, 2018 0
New Here ,
Mar 28, 2018

Copy link to clipboard

Copied

Works like a charm.  Thanks for all your help Thom!

here's roughly what I'm using.  will clean up, but this gets the job done.

function import_11() {

    try {

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

        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

        });

    }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 28, 2018 0