cancel
Showing results for 
Search instead for 
Did you mean: 

How to validate File Path given for upload

Former Member
0 Kudos

Hi All,

I am trying to upload a file using the File Upload Control. I have already bound the data and resource property with the context attributes. Can someone please suggest that what can be done to validate the file path? For eg. if i enter "abcd" in the file path and click a button for uploading the file. My application simply "<b>HANGS UP</b>" without even reaching the code what i have written on action of the button.

Is there any way to permit the user to enter the path only through Browse button and not manually? Please reply ASAP.

Thanks and Regards,

Smriti

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Smriti

Use this code in action button for upload a file

InputStream text = null;

int temp = 0;

try

{

File file = new File(wdContext.currentContextElement().getUp().getResourceName());

FileOutputStream op = new FileOutputStream(file);

if(wdContext.currentContextElement().getUp()!=null)

{

text = wdContext.currentContextElement().getUp().read(false);

while((temp=text.read())!=-1)

{

op.write(temp);

}

}

op.flush();

op.close();

String path = file.getAbsolutePath();

wdComponentAPI.getMessageManager().reportSuccess(path);

}

catch(Exception e)

{

e.printStackTrace();

}

Former Member
0 Kudos

Hi Satish,

Thanks for your reply. Can you please tell me that in the line:

wdContext.currentContextElement().getUp().getResourceName();

what is the method getUp() for? or is it the place holder for some context attribute? Please clarify.

Regards,

Smriti

arun_srinivasan
Contributor
0 Kudos

Hi smriti

Check out this thread

Regards,

Arun

Answers (1)

Answers (1)

Former Member
0 Kudos

hi Smiriti,

up is a context attribute of type "com.sap.ide.webdynpro.uielementdefinitions.Resource"

It is to be bind with upload UI element property called "resource"

Former Member
0 Kudos

Hi Sathish,

If i write this line:

File file = new File(wdContext.currentContextElement().getResource());

where "Resource" is the context attribute to which i have bound the "Resource" property of File Upload control

it gives me an error:

"The constrcutor File(IWDResource) is undefined"

Regards,

Smriti

Former Member
0 Kudos

Hi Smriti,

For the context attribute Resource you set the property "type" as "com.sap.ide.webdynpro.uielementdefinitions.Resource"

Then u bind that attribute to Upload UI element property "resource".After that u write this code

File file = new File(wdContext.currentContextElement().getResource().getResourceName());

Former Member
0 Kudos

Hi Sathish,

I tried to paste this code in the Action method of the Upload button but the application still hangs. I tried to debug, but the sad part is that control never goes this method. The application keeps on being in wait state even if i terminate the thread from debug mode.

Regards,

Smriti.

Former Member
0 Kudos

The application hangs up when i give the path as some junk data like "abcd"

Former Member
0 Kudos

Hi Smriti,

If u want to use Up load UI element you should enter like this(c:\ abcd(or) any directory:\filename).Otherwise it will hang.

Even if the file given is not there it will create a file and upload it.

Former Member
0 Kudos

Hi Sathish,

I want that even if customer enters file path as "abcdefg", an error should be thrown. Is there any way to do so? I am unable to catch this exception beacuse when a path something like "andbevd" is entered, the application simply hangs up irrespective of whatever code i write in the "Upload Button"'s action method.

Please suggest if there is some way to restrict the user from entering in the input field directly, I mean he should be able to enter the path only by choosing from the "Browse" button.

Thanks and Regards,

Smriti

Former Member
0 Kudos

Hi Smriti,

The definition of File Up load UI element is given below

You can use the FileUpload UI element to upload files from the client to the server. The UI element appears with <b>an input field, in which the directory path and the file name appear, and a button</b> for searching for the file.

From this i think we must give the directory name.

Former Member
0 Kudos

Hi Sathish,

Thanks a lot for your help. I will try and convince my project lead with the same reply. :)Hopefully, this helps. Otherwise I may need to find some other solution to it.

Thnaks and Regards,

Smriti.

Former Member
0 Kudos

Hi Smriti,

why don't u validate with the resourcename (i.e., your directory path)... get the resourcename in a string and check with the string is there any characters with (: or / etc... as it is mandatory for any directory path and then finally upload the file...

if the user enters "abc" over the input field in FileUpload UI, it will throw an error as u can catch it and throw ur message. Or else u can specify a default path which can be binded with the resource path during the execution...

Regards / Guru

Former Member
0 Kudos

Hi Guru,

I am not able to validate the string because the control doesn't reach the action method of the button. Can you please suggest where should i write the validation code?

Regards,

Smriti

Former Member
0 Kudos

Hi Smriti,

Have a look on the Following Links... which clearly briefs what is the use of FileUpload UI and how it works behind the screen...

<a href="/people/dagfinn.parnas/blog/2004/08/15/inputfile-how-to-hide-the-plain-browse-button you about the how the FileUplaod UI is being built.</a>

<a href="/people/dagfinn.parnas/blog/2004/08/15/inputfile-how-to-hide-the-plain-browse-button more stuff on FileUpload UI</a>

<a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;892442">How the FileUpload UI interacts with the local system</a>

After going thru above post, you can identify that the inputField is being declared as FILE type (like <input type="file" >), and you would also noticed the cause for your error in the last link which I have specified above, and what microsoft also recommends for it as a solution to show an alternative message in place of the blank screen...

Hope now you would be clear...why your method is not being invoked..

Please revert back for futher queries... try to come soon with an alernative solution...

Regards / Guru

Message was edited by:

Armin Reichert

Former Member
0 Kudos

Hi Guru,

Thanks for your reply. I have understood the concept, but the issue is that i am not able to disable the input field in Web DynPro for Java and force the user to enter through the Browse button only. Can you please provide me sample code to do the same?

Thanks and Regards,

Smriti.

Former Member
0 Kudos

Hi Smriti,

Well, you have one more option where u can first acknowledge the User whenever he opens the View.

For Eg, When the user navigates to the FileUpload View, We make focus on the InputField of the FileUploadUI and pass a message to the User, saying that"Specify the complete path if it thru manual process..." or something else...

To do that,

Navigate to wddomodifyview(), and paste the following code...

IWDFileUpload messagespot=(IWDFileUpload)view.getElement("FileUpload");

messagespot.requestFocus();

Thus, the inputField will be highlighted with a user message either from Message Pool or normal messages like...

wdComponentAPI.getMessagemanager.reportSuccess("Please enter the complete path of thee file if it is manual process...") in the wdDoinit() method...

by this way we acknowledge the user... will try is there could be any other solution...

Regards / Guru