cancel
Showing results for 
Search instead for 
Did you mean: 

FileUpload - Checking Resource data prior to upload

Former Member
0 Kudos

Hi All,

Is there a way of examining the Resource data before a file is uploaded, i.e. to prevent them uploading files above a certain size or with a certain extension? I can't see how I can do this until the file has already been uploaded.

Cheers,

Steve

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

// for checking the size of the resource use the following code.

public byte[] getByteArray(InputStream ipStr) {

byte[] bytArr = null;

try {

bytArr = new byte[ipStr.available()];

} catch (IOException e) {

e.printStackTrace();

}

try {

ipStr.read(bytArr);

} catch (IOException e1) {

e1.printStackTrace();

}

return bytArr;

}

// getting the resource that user has browsed

IWDResource resource = element.getResDocumentUploaded();

// Getting the byte array of the resource selected

byte[] arrBIN = getByteArray(resource.read(false));

//checking If file size is less than 25 MB

if(arrBIN.length < 25 * 1024 * 1024)

{

}

Using this code we can check if file uploaded is less than 25MB

################################################

for getting file extension of document uploaded one can use the following code

ublic static java.lang.String getFileExtension(

java.lang.String strResourceName) {

String strExtension = null;

int iIndex = strResourceName.lastIndexOf(".");

strExtension = strResourceName.substring(iIndex + 1);

return strExtension;

}

IWDResource resource = element.getResDocumentUploaded();

String strResource = resource.getResourceName();

String strExtension = getFileExtension(strResource);

one can create another input fiels wich takes in the extension of the uploaded file and compare it with strExtension

string application = context_of_field acceptingextension

application.equalsIgnoreCase(strExtension)

Regards

Rohan.

Please do provide me points if this helps you .Thank you

Edited by: rohan Henry on May 27, 2008 11:05 AM

Edited by: rohan Henry on May 27, 2008 11:08 AM

Edited by: rohan Henry on May 27, 2008 11:09 AM

Former Member
0 Kudos

bytArr = new byte[ipStr.available()];

this is actually

bytArr = new byte (open square bracket) ipStr.available() (closed square bracket) ;

Edited by: rohan Henry on May 27, 2008 11:11 AM

Answers (1)

Answers (1)

former_member182374
Active Contributor
0 Kudos

Hi,

There is a difference between uploading the file to the context and saving it in the file system.

Say you are working in NW2004, you have a fileResource (type byte) in your context.

fileResource is bound the file upload element.

When user uploads a file it is saved in the context.

From there, you can check it size (something like wdContext.currentContextElement().getFileResource().length

According to the length you can save it in the file system or display an error message regarding its size...

Regards,

Omri

Former Member
0 Kudos

Hi,

Thanks for the reply. I did actually mean before uploading into the context. My concern is that users will be loading scanned documents, whithout understanding how big they are, and the server won't have enough memory to handle all the concurrent requests.

Cheers,

Steve

Former Member
0 Kudos

Hi,

If you want the validation to take place in the clide side its not possible with webdynpo as we dont have access to the javascript side of WD.

An alternative is to use jsp for this purpose.

Regards

Ayyapparaj