cancel
Showing results for 
Search instead for 
Did you mean: 

File Upload Problem

Former Member
0 Kudos

Dear SDN Community,

I'm trying to upload a file to a server but the created file is empty.

I don't know how to bind the data file to the created file, I'm using this code:

Context atributtes

ResourceUp of type Resource bind to resource property of UploadFile

UploadStream of type Binary bind to data property of UploadFile

Code:

IPrivateP_PR_03.IContextElement element = wdContext.currentContextElement();

// if a file in the FileUpload field exists

if (element.getResourceUp() != null) {

IWDResource resource = element.getResourceUp();

// get the size of the uploaded file

element.setFileSize(this.getFileSize(resource));

// get the extension of the uploaded file

element.setFileExtension( resource.getResourceType().getFileExtension());

// set context attribute 'fileName' .

element.setFileName(resource.getResourceName());

String location = "/home_dev/sr1/dympro/";

String file = resource.getResourceName();

String filename = location+file;

File destinationFile = new File(filename);

try{

FileOutputStream out = new FileOutputStream(destinationFile);

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

out.write(wdContext.currentContextElement().getUploadStream());

out.flush();

out.close();

}catch(Exception e){

messageMgr.reportException(e.getMessage(),false);

}

A prompt reply would be very much appreciated : )

Regards,

Accepted Solutions (1)

Accepted Solutions (1)

arun_srinivasan
Contributor
0 Kudos

Hi

Upload the file using the File Upload component and bind a value attribute of type binary to the file upload UI.Create a button and assigna action>let the action for the button be submit.

Code:

public void onActionSubmit(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionSubmit(ServerEvent)

IWDAttributeInfo attInfo =wdContext.getNodeInfo().getAttribute("<value attribute name>");

IWDModifiableBinaryType binaryType;

binaryType =(IWDModifiableBinaryType) attInfo.getModifiableSimpleType();

fileuploaded = binaryType.getFileName();

byte b[] = wdContext.currentContextElement().get<value attribute name>();

File filename =new File("

sharelocation"+fileuploaded);

try {

FileOutputStream out = new FileOutputStream(filename);

out.write(b);

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

catch (IOException e) {

e.printStackTrace();

}

//@@end

}

hope this helps

Thanks and Regards,

Arun

Former Member
0 Kudos

Hi Arun,

I have the same problem with your code,

upload = binaryType.getFileName() // is empty

byte b[] = wdContext.currentContextElement().getUpload(); // is empty too

A have defined a atributte of type binary (Upload) and I have bind it to the Upload File data property

you know if i can fill b[] from Upload File resource property?

Thanks and Regards,

Juan Luis

arun_srinivasan
Contributor
0 Kudos

Hi

What type of file you are trying to upload like xls,csv,txt

Thanks and Regards,

Arun

Former Member
0 Kudos

Hai,

byte[]=wdContext.currentContextElement().getResource().read(true);

Regards,

Naga

Former Member
0 Kudos

Hi, I'm uploading documents of type .txt .doc .xls .pdf ....

Naga this line

byte[]=wdContext.currentContextElement().getResource().read(true);

give me an error because wdContext.currentContextElement().getResource().read(true); returns a Inputstream and I need a type byte.

I have tried this:

byte b[]=null;

InputStream inputS = wdContext.currentContextElement().getResource().read(true);

inputS.read(b);

File filename =new File("/home_dev/sr1/dympro/" + file);

FileOutputStream out = new FileOutputStream(filename);

out.write(b);

out.flush();

out.close();

But this code give me an Null pointer exception at out.write(b);

Thanks and Regards,

Juan Luis

arun_srinivasan
Contributor
0 Kudos

Hi

File filename =

new File("

<sharename>
<foldername>
<foldername>
"+fileuploaded);

eg.

normal os access like this
sharename\uoload\filename.txt

we should specify like this in webdynpro



sharename
upload
filename.txt

here \ is escape sequence

Hope this helps,

Regards,

Arun

Former Member
0 Kudos

Hai,

FileInputStream inputS = wdContext.currentContextElement().getResource().read(true);

FileChannel srcChannel = fis.getChannel();

// Create channel on the destination

FileChannel dstChannel = new FileOutputStream(("/home_dev/sr1/dympro/" + file).getChannel();

// Copy file contents from source to destination

dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

// Close the channels

srcChannel.close();

dstChannel.close();

Regards,

Naga

Former Member
0 Kudos

Hi Naga,

If I try tihs line :

FileInputStream inputS = wdContext.currentContextElement().getResource().read(true);

it give me an error because

wdContext.currentContextElement().getResource().read(true) is InputStream type

and InpoutS is FileInputStream type.

Thanks and regards,

Juan Luis

Former Member
0 Kudos

Hi,

FileInputStream inputS =(FileInputStream) wdContext.currentContextElement().getResource().read(true);

Regards,

naga

Former Member
0 Kudos

Thanks Naga.

It resolves my problem.

Thanks and regards.

Juan Luis

Answers (3)

Answers (3)

Former Member
0 Kudos

Hello Luis,

Check out this link... this may help you..

http://www.sapdevelopment.co.uk/file/file_updown.htm

Regards,

Arun

Former Member
0 Kudos
Former Member
0 Kudos

Hi,

Have a look into this tutorial... it will clear all your doubts regarding uploading and downloading file using webdyunpro for JAVA....

<a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/202a850a-58e0-2910-eeb3-bfc3e081257f">Uploading and Downloading Files in Web Dynpro Java</a>

Please reward points if it helps..

Thanks and Regards

Avijit