NetTalk Central

Author Topic: Code executed 3 times  (Read 4454 times)

Keith

  • Sr. Member
  • ****
  • Posts: 306
    • View Profile
    • Email
Code executed 3 times
« on: February 08, 2015, 12:37:37 AM »
Hi

I am uploading photo files and changing the name to render them unique.  I noticed (by inserting trace) that  the code which saves the path and file name (SaveFile::FND:Photo1  Routine) executes three time for each upload.  It looks as if the name is the same each time i.e I change it but on the second entry it is back to the original (the \web\uploads\<fname>.jpg) so there isn't any harm done.

But .. . is this anything to worry about and why is it happening  (maybe I've done something wrong)?

Cheers

Keith
Clarion 10.0.0 build 12799
NetTalk 9.31
StringTheory 2.65
InsightGraphing 2.27

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11191
    • View Profile
Re: Code executed 3 times
« Reply #1 on: February 08, 2015, 09:48:56 PM »
Hi Keith,

ha!  I scoff at your prehistoric version. 8.32 is just sooo last year! :)

(There was a tweak in 8:33 - from the release notes;
Fix: Call to SaveFile removed from Validate routine (already in ValidateValue)
)

It should only be called 2 times, not 3 - once when the file is uploaded and once when the user clicks on the "Save" button. (In only 1 of the 2 cases will it actually do anything, but which time it actually saves depends on the uploader itself, and if you are storing in a file or a blob.)

Cheers
Bruce

Keith

  • Sr. Member
  • ****
  • Posts: 306
    • View Profile
    • Email
Re: Code executed 3 times
« Reply #2 on: February 09, 2015, 12:42:52 AM »
Hi Bruce

Ok.  I have upgraded to 8.34 and STheory 2.14 and it's better but no cigar  >:(

What I have done is in the 'SaveFile::FND:Photo1  Routine' (my upload field is FND:Photo1) I am changing the name that is already loaded into FND:Photo1.  I am successfully randomising the name and the new unique name is in FND:Photo1.  So far so good.

BUT, as you said, the code still executes twice but my code also runs twice.

The first time the code runs I change the name of the file from Pet1.jpg (ignoring the path) to ABPet1.jpg and the second time that name gets changed to PPABPet1.jpg. 

The file gets written to the \uploads folder as ABPet1.jpg but the name stored in the table is PPABPet1.jpg

On the File Upload field I just have 'Save file to uploads folder' ticked, no Blob, no 'Remove'.  You said that in only one of the two cases will it do anything but this seems not to be the case.

Could we need an 8.35 :)

Cheers

Keith
Clarion 10.0.0 build 12799
NetTalk 9.31
StringTheory 2.65
InsightGraphing 2.27

Keith

  • Sr. Member
  • ****
  • Posts: 306
    • View Profile
    • Email
Re: Code executed 3 times
« Reply #3 on: February 15, 2015, 01:20:50 PM »
Hi Bruce

The SaveFile code seems to be  executed once after the upload and once on 'Save'.

I have fudged the result in the code by testing to see whether I have been there before and if so then doing nothing.  So I now have code that works but its icky.

Any comments?

Thanks

Keith
Clarion 10.0.0 build 12799
NetTalk 9.31
StringTheory 2.65
InsightGraphing 2.27

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11191
    • View Profile
Re: Code executed 3 times
« Reply #4 on: February 15, 2015, 09:56:42 PM »
hmm - I thought I replied to this thread, but obviously not...

The key is to wrap your code in a test to see if the file is actually included in the current request. (it may arrive as an async upload, it may arrive as part of the POST, so you should cope with it whenever it actually arrives.

The best thing to do is just check the GetValue;

l = self.GetValue(lower('fieldname_length')

where fieldname is the name of the file-upload field.

Cheers
Bruce

Keith

  • Sr. Member
  • ****
  • Posts: 306
    • View Profile
    • Email
Re: Code executed 3 times
« Reply #5 on: February 22, 2015, 10:23:27 PM »
Bruce

Sorry I have to return to this but I'm stuck.

The code in SaveFile gets executed on 'Save' for the Form (as well as after an Upload) so, as you said, you have to decide whether to execute the 'change name' code or not.

But I don't know what:

l = self.GetValue(lower('fieldname_length')

means and got compile errors  'Field not found:  GETVALUE' when I tried to nut out what it might be.

Could you spell out exactly what the 'test' is that goes around my code (which is just to execute a procedure).

Thanks

Keith
Clarion 10.0.0 build 12799
NetTalk 9.31
StringTheory 2.65
InsightGraphing 2.27

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11191
    • View Profile
Re: Code executed 3 times
« Reply #6 on: February 23, 2015, 01:34:23 AM »
Sorry Keith, my mistake - I took that code from the netweb.clw and didn't adjust it for embedding. It should read p_web not self. ie

result = p_web.GetValue(lower('fieldname_length'))   ! added missing bracket

you also need to declare result

result  long

in the procedure, or routine, where it is used.

fieldName in this context is the fieldname of the field you are fiddling with. ie the fieldname of the file upload field. (if the fieldname and equate are different you _may_ need to use the equate - I haven't checked.)

Cheers
Bruce


« Last Edit: February 23, 2015, 10:36:08 PM by Bruce »

Keith

  • Sr. Member
  • ****
  • Posts: 306
    • View Profile
    • Email
Re: Code executed 3 times
« Reply #7 on: February 23, 2015, 03:34:33 AM »
Bruce

Not sure what I'm doing wrong here.  I have got this line of code:

result = p_web.GetValue(lower('FND:Photo1_length')

FND:Photo1 is my Upload field name.  The Equate is the same name.

I get the compile error:

'Expected: <operator> ), &={'

see attached pic.

Keith


[attachment deleted by admin]
Clarion 10.0.0 build 12799
NetTalk 9.31
StringTheory 2.65
InsightGraphing 2.27

kevin plummer

  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
    • Production Accounting and Software Payroll
Re: Code executed 3 times
« Reply #8 on: February 23, 2015, 02:22:08 PM »
looks like you are just missing a bracket off the end. Also I think the names of stored fields are case insensitive so you could lose the lower

p_web.GetValue('FND:Photo1_length')

Keith

  • Sr. Member
  • ****
  • Posts: 306
    • View Profile
    • Email
Re: Code executed 3 times
« Reply #9 on: February 23, 2015, 06:30:06 PM »
Kevin

Ha! Missing a bracket indeed - and after all the drama . . . !

Of course you are right.

These file uploads work perfectly now after testing the 'result but this 'wrapper' code needs to go into the 'Developing Web Applications' book together with an explanation.

Cheers

Keith
Clarion 10.0.0 build 12799
NetTalk 9.31
StringTheory 2.65
InsightGraphing 2.27

Keith

  • Sr. Member
  • ****
  • Posts: 306
    • View Profile
    • Email
Re: Code executed 3 times
« Reply #10 on: February 24, 2015, 01:40:06 AM »
Bruce et alia

Hi. There is one other point to report.

I had been doing all of this upload and file name change testing with action=change on the Form.  After getting the name change working today I grafted the code into a Form which always starts in 'Insert' mode and the photo file name did not get changed  ???

I found out that after the routine that creates the unique name is executed, for action=Insert, you have to set the photo name field session variable.  For action=change you don't.

Without the p_web.ssv, in the 'insert' case the file gets uploaded to the \web\uploads folder with the correct (unique) name but the path name in the table field is the unchanged name.

Cheers

Keith

Clarion 10.0.0 build 12799
NetTalk 9.31
StringTheory 2.65
InsightGraphing 2.27