Expand my Community achievements bar.

SOLVED

Remove file attachment button does not work

Avatar

Level 9

I have a form with an area the user can attach files. There are three buttons. One to add a file, one to open the file to view it and the third button to remove the file that is highlighted in the listbox. The remove button is not working. Can you see anything wrong with my script?

ADD FILE SCRIPT

  MeetingMinutes.LogoControls.Attachments.Button1::click - (JavaScript, client)
var myDoc = event.target;
var sFile = "myFile" + NumericField1.rawValue;
myDoc.importDataObject({cName: sFile});
var myDataObject = myDoc.getDataObject(sFile);

var sFileName = myDataObject.path;

ListBox1.addItem(sFileName,sFile);

NumericField1.rawValue = NumericField1.rawValue + 1;

OPEN FILE SCRIPT

MeetingMinutes.LogoControls.Attachments.Button2::click - (JavaScript, client)
try{
var myDoc = event.target;
var sFile = ListBox1.rawValue;

myDoc.exportDataObject({ cName: sFile, nLaunch: 2 });
}
catch(e)
{
app.alert("Click on the file you want to open (select it)," + "\n" + "then click the Open Selected File button");
}

REMOVE BUTTON SCRIPT

MeetingMinutes.LogoControls.Attachments.Button3::click - (JavaScript, client)
try {
var myDoc = event.target;
var sFile = ListBox1.getDisplayItem(ListBox1.selectedIndex);

if (sFile !== "") {
  var nResponse = xfa.host.messageBox("You are about to delete the highlighted attached file. \n\nDo you want to continue?", "Deleting an Attachment", 1, 2);
  if (nResponse == 4) {
   myDoc.removeDataObject(sFile) + ListBox1.deleteItem(ListBox1.selectedIndex) + (NumericField1.rawValue = NumericField1.rawValue - 1);
  }
} else {
  xfa.host.messageBox("No document selected. Click on the file you want to delete.");
}
} catch (e) {
xfa.host.messageBox("Error in removing document");
}

1 Accepted Solution

Avatar

Correct answer by
Level 8

Change var sFile = ListBox1.getDisplayItem(ListBox1.selectedIndex);

to

var sFile = ListBox1.rawValue;

Kyle

View solution in original post

11 Replies

Avatar

Level 8

The line:

myDoc.removeDataObject(sFile) + ListBox1.deleteItem(ListBox1.selectedIndex) + (NumericField1.rawValue = NumericField1.rawValue - 1);

should be:

myDoc.removeDataObject(sFile);

ListBox1.deleteItem(ListBox1.selectedIndex);

NumericField1.rawValue = NumericField1.rawValue - 1;

Kyle

Avatar

Level 9

Kyle,

I made the changes you recommended but the "Delete" button still does not delete the attachment from the paperclip. Please see attached file...

https://workspaces.acrobat.com/?d=S4lZ600mLDmsIik-4zcDBA

Avatar

Correct answer by
Level 8

Change var sFile = ListBox1.getDisplayItem(ListBox1.selectedIndex);

to

var sFile = ListBox1.rawValue;

Kyle

Avatar

Level 9

Kyle,

I added three documents and then deleted one. When I went to add another document, I got the error below... (thanks for your help)

Picture1.jpg

Avatar

Level 8

I'm not sure exactly why your getting that error but it could have something to do with the way you name your attachments. If you assign unique names by adding an incrementing number what happens is this:

1)Add a file (myFile1)    counter=1

2)Add a file (myFile2)     counter=2

3)Delete myFile1      counter=1

4)Add a file  (myFile2)   counter=2  <------Now you're trying to add a file with the same name of one that is already attached!

Here's what I do:

  Replace

  sFile = "myFile" +NumericField1.rawValue;

with this

sFile = "myFile"+Math.round(Math.random()*10000000000000);//Randomly generated number

See if that works.

Kyle

Avatar

Level 9

That sounds like a great idea. I will try it out when I get back to work on Monday and post if it solves my error. Thanks so much for your help.

Avatar

Level 9

Kyle,

Your solution worked great. Thank you so much for sharng your knowledge and experience.

Note: You know more about this than I do but I was wondering if rather than using a radomly generated number, what if I use miliseconds like I do when auto-numbering a form to prevent a duplicate file name?

Something like:

var d = new date()

sFile =  Math.floor(d/1000); //divide by 1000 to get seconds and trim decimals

       

Just a thought... I would be interested in your comments.

Thanks again Kyle

Avatar

Level 8

Yup, that would work too.

Kyle

Avatar

Level 9

Kyle,

Do you think the milliseconds would be better to use and make it impossible to have a duplicate file name or should I stick with the radomly generated number that you have experience with?

Thanks for your feedback.

-Don

Avatar

Level 8

I like your millisecond approach. Go with that.

Kyle

Avatar

Level 9

Kyle,

I'll give it a try. Thanks for your help with this.

-Don