Page 1 of 2

Javascript question re. File Upload Widget

Posted: Fri Jun 27, 2014 1:33 pm
by Wayne C.
This is more of a question for the Genie user community since my functional familiarity with javascript is limited. I'm researching this on my own but maybe somebody out there could give me a hand.
I'm using the File Upload widget to upload a picture a driver would take of an order he is picking up from a warehouse. The widget is placed a on a screen where our order number is displayed. Using this bit of script in the "rename to" property of the widget...
script: Name = get('I_1_10') + '.jpg';
... I'm able to change the file name of the picture to the order number. This works. What we'd really like to do is to create a folder that is named the order number (if it does not already exist) and then upload the above named JPEG file into that folder. (Elsewhere in our system, we'd like to be able to upload documents associated with that order into the folder.)

I'm guessing this is might be a function that I would need to add to the "customjs" file in my Genie skin that would be called from the widget upon clicking on "Upload". If this is a relatively simple procedure, any help or tips in creating this code, where it should be placed or how it is called would be appreciated.

Re: Javascript question re. File Upload Widget

Posted: Mon Jun 30, 2014 10:40 am
by Scott Klement
It looks as though your order number is an input field that's on the same screen as the upload widget. Is that what you meant to do? The reason I ask is that it would allow the user to change the order number on the screen and then upload files to that order number without the order number before the order number is validated by the RPG program. This could potentially be a security hole. I hope you see what I mean?

I would think it would be a better idea to have a function key (with a button in Genie) that opens a separate screen for the upload. On this separate screen the order number can be an output-only field. This would give you the opportunity to validate the order number before using it in the path name.. If you do that, then you'd want your RPG program to create the IFS directory once the order number has been validated, before displaying the screen with the file upload widget on it.

You cannot create directories on the server from JavaScript, as JavaScript runs in the browser (not on the server). So your idea of using custom.js would not work, at least not directly. You could potentially have JavaScript that runs a server-side program via AJAX, of course... but this seems like a very complex solution. I would just create the directory from the green-screen program that's displaying the screens, it would be much simpler. (Unless you don't have the source code, that is.)

Re: Javascript question re. File Upload Widget

Posted: Mon Jun 30, 2014 1:00 pm
by Wayne C.
Thanks Scott for your reply. After doing some research after I posted my question, I found out that you can't create directories from javascript. Rats.

As you said, the order entry screen where we placed the widget has our pro # (order #), but it is protected. By the time our drivers would see the order, the pro # is protected. We did a quick test a few weeks ago where we took a picture of something and the file appeared immediately in the file upload widget but with a random file name. Because this part of the process is done by truck drivers (with various levels of computer experience), we're trying keep their roles in this very simple. We just want them to take a picture and then tap "Upload". Behind the scenes, we're going to change the file name and create the appropriate directories. So far, I've been able to change the file name, but not create the directory.

It sounds like creating the IFS directory in the RPG program is the way to go. Thanks for the suggestion. That's why you're our hero over here;)

Re: Javascript question re. File Upload Widget

Posted: Mon Jun 30, 2014 4:17 pm
by Scott Klement
Yeah, if it's protected, it should work fine.

You'll want to set the target directory property to something like (change to the appropriate IFS location)

Code: Select all

script: "/uploads/" + get("I_1_10")
This way, the directory name will include the order number. Also, I just noticed that you are assigning a variable in the 'rename to' property. Seems like it shouldn't have the "Name =" in the expression, but instead should be something like this:

Code: Select all

script: get("I_1_10") + ".jpg";

Re: Javascript question re. File Upload Widget

Posted: Thu Jul 24, 2014 5:07 pm
by Wayne C.
Using your advice, I've got this puppy working... with 1 obstacle remaining and I'm not sure how to handle it. This is the format of our order / pro #: AAA 9999999 99. (A 3 character alpha-numeric branch code, a 7 digit pro #, a 2 digit suffix). On the display, we edit (zero-suppress) the 7 digit pro # and the 2 digit suffix. The pro # is generally 7 significant digits (ex. 2247139) so the editing is somewhat irrelevant. But the suffix 97% of the time is 00. Because the edited suffix appears as blanks, and I concatenate those 3 fields to construct the directory/folder, I get a message ("the directory cannot be found") when I attempt to "Upload" the file. (For testing purposes, I hardcoded 00 into my Genie side directory and was able to upload a file to the proper folder.) The folder was created as TMI224713900.

The easiest way (in my opinion) to rectify this would be to simply remove the editing from the suffix field. Our programmer, however, is resistant to the idea because he believes the users will be confused when they see the 00. He also tried creating the folders with the edited pro # and suffix, but I got the above message when trying to upload.

I was wondering if you have any suggestions. Pete the programmer asked if we could use some javascript code (or something else) to substitute zeros for the blanks at some point before trying to upload the file.
Thanks for any guidance.

Re: Javascript question re. File Upload Widget

Posted: Thu Jul 24, 2014 5:45 pm
by Scott Klement
Yep, that sounds like the way to do it... write yourself a Javascript routine that formats your pro# exactly the way it should be for an IFS directory. Then, just call that routine from your Genie screen.

One easy way to make that JS function available from everywhere is to stick it into your custom.js file. (Or, if you have a whole library of them, you might want to just create your own JS file and just link it into your start.html)

Unfortunately, I won't have time today to code up an example, if you think one would be helpful, perhaps I could do that tomorrow? Let me know.

Re: Javascript question re. File Upload Widget

Posted: Fri Jul 25, 2014 12:54 pm
by Wayne C.
I have been looking on the web for some examples and some javascript tutorials. None of us here have any real experience in javascript so some sample code or a skeleton (at your convenience) would be much appreciated. There's no real rush for this and in the meantime, I'm going to continue get a better handle on js.

Re: Javascript question re. File Upload Widget

Posted: Fri Jul 25, 2014 2:51 pm
by Scott Klement
I have some questions about the format of this pro#.

I understand that the format is AAA 9999999 99.

-- The first 3 digits are alphanumeric (Can be letters or numbers) and are always present.
-- The next 7 are numeric only, and are zero suppressed.
-- The 2 digit suffix is usually 00, and is also zero suppressed.

With that in mind, here are my questions:

1) Is there a space between the 3-digit start code and the numeric code? (That's what the above diagram shows.)
2) Likewise, is there a space between 7-digit number and the 2-digit number?
3) When you zero suppress, are the numbers stripped off entirely? Or are they just converted to blanks?

For example, if you had ""XXX 0034567 09" would that be displayed as "XXX345679"? Or would it be "XXX 34567 9"? Or how does that work?

Re: Javascript question re. File Upload Widget

Posted: Fri Jul 25, 2014 5:33 pm
by Wayne C.
Here are some answers for you.

1. Yes, there is a space between each of the 3 elements of the order. They are 3 different fields within our file.
2. Ditto
3. The zeros in the numeric fields are just converted to blanks. Using your example the order is displayed
XXX ..34567 .9
I used periods (as placeholders) to account for spaces that would appear due to editing in addition to the single space between each element.

Re: Javascript question re. File Upload Widget

Posted: Fri Jul 25, 2014 5:54 pm
by Scott Klement
Oh, these are separate fields? My apologies, I thought it was just one field (I_1_10).