Expand my Community achievements bar.

script to remove attachments

Avatar

Former Community Member

Hi All,

Can anybody provide me the script to remove all the attachments from the form?

Pls help its urgent.

Thanks

Abhiram

8 Replies

Avatar

Level 10

Hi,

Paul has a sample on the forums for working with attachments - try a search.

Attachments are dataObjects, so if you check the syntax you can work with them:

var myDoc = event.target;

myDoc.removeDataObject("the full file name of the attachment.pdf);

Good luck,

Niall

Avatar

Former Community Member

Niall,

I have tried similar script but no luck.. The script i have used for a click of a button.

var d = null;

d =  event.target.dataObjects;

if(d!=null)

        {

        for (var i = 0; i < d.length; i++)

                        {

                        try{

                                 var myDataObject = myDoc.getDataObject(d[i].name);

                                 var myDoc = event.target;

                                 var sFileName = myDataObject.path;

                       myDoc.removeDataObject(sFileName); //I am getting the name of the file here

                                 }

                                catch(e)

                                {

                                app.alert("Error in removing document");

                                }

                                xfa.layout.relayout();

                        }

        }

Basically, I want to remove all the attached docs from the form.

Pls help.

thanks

Abhiram

Avatar

Level 10

Hi Abhiram,

Try this:

// Decalre some variables

var oAttachments;

var myDoc = event.target;

var sCurrentAttachment;

var sFileName;

var i; 

// Get the attachments

oAttachments =  myDoc.dataObjects;

// Test to see if there are attachments

if(oAttachments.length != 0) {

          for (i=0; i<oAttachments.length; i++) {

          // Try and remove attachment

                    try {

                              sCurrentAttachment = oAttachments[i];

                              sFileName = sCurrentAttachment.path;

                              myDoc.removeDataObject(sFileName);

                    }

                    catch(e) {

                              app.alert("Error in removing document");

                    }

          }

}

Hope that helps,

Niall

Avatar

Former Community Member

Still no luck Niall..

The similar code is working in other place wherte we have to remove a particular attachment. but its not working for the entire attachments in the form.

Just a question...What does this event.target do?

Thanks

Abhiram

Avatar

Level 10

Hi Abhiram,

I have tried it here and it seems to work okay. Have a look here: http://assure.ly/w1gjwC.

An XFA Form is in a wrapper so that it can be opened in Acrobat/Reader. When you are script inside the XFA Form, "this" refers to the object that contains the script. However in Acrobat, "this" refers to the document.

Now, when we are scripting to access the attachments (eg dataObjects) we are actually accessing Acrobat functionality, not XFA Form functionality. Therefore to access the Acrobat methods you need to preface the method with "event.target". We do this at the start of  the script and assign it to a variable "myDoc". Then every time we need to use an Acrobat method in the script we can use myDoc. For example, myDoc.dataObjects;

Hope that helps,

Niall

Avatar

Former Community Member

Hi Niall,

This works fine when we add attachments from the attachment panel but in my case the attachment gets added by a click of a button. It doesn't work here.

I am unable to find the difference between the both.

This is the code in my add attachment button:

var myDoc = event.target;
var sFile = "myFile" + Page1.attachmentCounter.rawValue;
myDoc.importDataObject({cName: sFile});
var myDataObject = myDoc.getDataObject(sFile);
var sFileName = myDataObject.path;
var count = ListBox.length;

var flag = 0;
if(count!=0)
{
for (var i=0; i<count; i++)
{
if (ListBox.getDisplayItem(i) == sFileName)
{
  flag = 1;
}
}
}
if (flag == 1)
{
myDoc.removeDataObject(sFile);
xfa.host.messageBox("This file is already attached. Please select another file");
}

if  (flag == 0)
{
ListBox.addItem(sFileName,sFile);
Page1.attachmentCounter.rawValue = Page1.attachmentCounter.rawValue+1;
}


I can send the file to your email if you need for your reference.

Pls. help

Thanks

Abhiram

Avatar

Level 10

Hi Abhiram,

I have added the removal all attachments button to Paul's example and I am seeing the same behaviour. Files that are manually attached can be removed with the above script, whereas files that are attached using script seem to not work.

I don't know why this is happening.

Using a console.println, I can see that the script works down to getting the name of the attachments. Its just the removeDataObject line that fires but does not actually remove the attachment.

I am at a loss!

Niall

Avatar

Former Community Member

The reason I am replying to such an old thread is this was the one that came up in Google trying to figure out the problem I was having. I have solved my problem. Maybe what I did may help the next person that finds this. The code snippet above deletes based on path. I don't think that is correct. Change to using the name attribute and I think it will work. My code to delete was based on description which worked only the time the form was open and the attachments added. Putting the attachments back on through a server side process failed. It appears to me that path coorelates to the name column when you view the attachments in Acrobat. Description is description. It also apears that removing attachments is based on the name attribute which is not displayed. When I add the attachments through code, the name attribute is set the same as the description attribute, the value I passed in. So I can delete them using the same string, description. I did not test this, but think that when added using the built in attachment icons, the name attribute is set the same as the path attribute, same as the name column in the display. So deleting by path attribute works because it is the same as the name attribute. When I added the attachments back using a render on the server, the name attribute got set to the path attribute (name column) with some numbers on the end. I ended up adding code to loop through the attachments, comparing the description to what I want to delete then deleting based on the name attribute of that item. This works for me both during the initial view of the form and after the attachments have been put back in in a server side render. You could also loop throuhg and compare path, then delete passing in the name attribute.

Another note about the code snippet to remove all attachments, the set being looped on is being altered during the loop. It might only delete every other attachment. It might delete 1 then go to 2 which used to be 3... Maybe always delete the first attachment and loop until there are no more attachments. Looping backwards should work too so it is deleting the last attachment in each iteration.