cancel
Showing results for 
Search instead for 
Did you mean: 

Webdynpro to limit MIME types and size

jerome_lim
Participant
0 Kudos

Hi,

I need to write an web dynpro application to limite the file type and size using IWDFileUpload API, does anyone has some suggestion ?

Thanks a lot.

Kind regards

Ben.J.

Accepted Solutions (0)

Answers (3)

Answers (3)

jerome_lim
Participant
0 Kudos

hi Abhinav,

Thanks for your answer.

Kind regards

Ben

Abhinav_Sharma
Contributor
0 Kudos

Hi Ben,

I think you can restrict the file type and and also check the file size before uploading the file. You need to write the logic to check out the file type.

Use the following code to check restrict the file upload of only txt file...

IPrivateTestView.ITestElement pf =

wdContext.nodeTest().currentTestElement();

IWDMessageManager mgr = wdComponentAPI.getMessageManager();

if (pf.getFileResource().getResourceType().getFileExtension() != null

&& pf

.getPatchResource()

.getResourceType()

.getFileExtension()

.equalsIgnoreCase(

"txt")) {

// Check if file size is in valid range.. call getFileSize()

mgr.reportSuccess("File can be uploaded");

} else {

mgr.reportException("Kindly upload ont TXT files", false);

}

And use following code for calculating File Size:

private String getFileSize(IWDResource resource) {

InputStream stream = null;

DecimalFormat myFormatter = new DecimalFormat("###.##");

double size = 0;

String unit = "";

try {

stream = resource.read(false);

size = stream.available();

if (size < 1024) {

unit = " Bytes";

} else if (size < 1048576) {

size = size / 1024;

unit = " KB";

} else if (size < 1073741824) {

size = size / 1024 / 1024;

unit = " MB";

}

} catch (IOException e) {

wdComponentAPI.getMessageManager().reportException(

e.getLocalizedMessage(),

true);

} finally {

if (stream != null) {

try {

stream.close();

} catch (IOException e) {

wdComponentAPI.getMessageManager().reportException(

e.getLocalizedMessage(),

true);

}

}

}

return myFormatter.format(size) + unit;

//@@end

}

This way you can restrict the users. As far as I know there is no standard way of restricting the same

Hope it helps

Regards

Abhinav

junwu
Active Contributor
0 Kudos

Hi,

i think file is already uploaded when your coding is executed.

Best regards,

John

junwu
Active Contributor
0 Kudos

it looks like impossible.

jerome_lim
Participant
0 Kudos

why not ?

Abhinav_Sharma
Contributor
0 Kudos

See my reply above

Edited by: Abhinav Sharma on Sep 24, 2010 1:57 PM