In a recent project, I found an “issue” with the upload control. When you press the button to upload, it does not send the value of controls to the server.
Let’s look at the General Tab of the direction for a normal button.
See the option for Send the value of controls to the server. That option is on by default when you create a button. Unfortunately the upload control doesn’t have that option, and it doesn’t send the value of controls to the server.
What exactly does that mean, why do you care?
What it means is any server code that is execute as part of the file upload may not have the correct values for the edit controls. In this project we are creating the filename based on some combo boxes and entry fields on the screen and using that name during the upload, using this code
EDT_fileName = COMBO_title..DisplayedValue +"_" +EDT_MSNUpload + "_" + DateToString(EDT_issuedDate,"YYYYMMDD") + "_" + SysDateTime() + "." + ExtractString(UploadFileName(UPL_Upload1,False),lastRank,".") UploadCopyFile(MySelf,fTempPath(),EDT_fileName)
Unfortunately we were not always getting the right values, it all depending on if we had done anything in another control that had an automatic AJAX action that was therefore Sending the value of controls to the server. So we were getting blanks in our filenames.
So how did we resolve the issue?
First we create a hidden button called btnSubmitValuesToServer, in fact that screen shot above is that button. Don’t forget to also AJAX enable the server code behind that button so that you don’t get a page refresh!
So now that we have a button that can be pressed to Send the value of controls to the server we just need to “press it”. We can’t very well ask the user to press an extra button before pressing the upload button, so we have to do it pragmatically, which we do with this line of code.
But there is one trick left. You might thing we could but that in the server code right before we name the file in the code shown above. The issue is that is server code so that doesn’t help us we need to do it from the browser before the server is every called. So instead we do it in the browser code of the upload button. Here is a screen shot of all the code behind our upload button.
And you can see where we “Press” our button during the browser code. Now when the server code executes, it has the right values for all the controls and we get the correct file name.
In case you are curious, I tested this with both the FLASH upload and the new HTML5 upload control and neither sent the value of controls to the server. Hopefully PCSOFT will add this an an option on the upload control in the future, but at least until that do we have a simple work around for the issue.