cancel
Showing results for 
Search instead for 
Did you mean: 

uploading text file in webdynpro view

ranjit_mohanty
Explorer
0 Kudos

Hi all,

I have to upload a text file (In which all field datas are separated by Tab) in webdynpro View.Please give me the details how to do it.

Thanks in Advance

regards,

Ranjit

Accepted Solutions (0)

Answers (4)

Answers (4)

ranjit_mohanty
Explorer
0 Kudos

I got the answer and solved it.Thanks Ajay.

Former Member
0 Kudos

Ranjit,

Please find the code below. Also, if you see any sysntax erros or typos, try to correct them when you implement this.

IWDResource resource = null;
String strFileExtn = null;
String strFileName = null;
InputStream inputStream = null;
FileOutputStream fout = null;
BufferedReader br = null;
File fp = null;
double fileSize = 0;
int ofst = 0;
int num = 0;
StringWriter sw = new StringWriter();
boolean returnState = true;

try
{ 
if(wdContext.currentContextElement().getFileResource() != null) 
{


resource = wdContext.currentContextElement().getFileResource();
strFileExtn = resource.getResourceType().getFileExtension().toUpperCase(); 
strFileName = resource.getResourceName();



// create an input stream to read the resource.
inputStream = resource.read(false);
// get the file size.
fileSize = inputStream.available();



// Create the byte array to hold the data
byte[] bytes = new byte[(int)fileSize];


// Read in the bytes
while (ofst < bytes.length && (num = inputStream.read(bytes, ofst, bytes.length-ofst)) >= 0)
{
ofst += num;
}



fp = new File(strFileName); 


fout = new FileOutputStream(fp);
fout.write(bytes);
fout.flush();
fout.close();



br = new BufferedReader(new FileReader(fp));
try 
{


String txt = null; 
int index = 0;
while (( txt = br.readLine()) != null)
{
//Get the Index for the 'Tab'
index = txt.indexOf('\t');
wdComponentAPI.getMessageManager().reportSuccess("Field -"+txt.substring(0, index) " +" Value -"+txt.substring(index,txt.length()));


}
}
catch (IOException ex)
{

}
}
catch(FileNotFoundException fe)
{


}
catch(IOException ioe)
{
}
catch(Exception e)
{
}

Regards,

Ajay

Former Member
0 Kudos

Hi Ranjit,

1.Add a 'file Upload' UI element to your Web Dynpro View.

2.Create a context attribute of type 'Resource' say 'FileResource' and bind it to the resource property of the file upload UI elemnt.

3. Create a button say 'Upload' and an action attached to it say 'Upload'

4.Write the following code in your 'Upload' action

IWDResource resource = null;

String strFileExtn = null;

String strFileName = null;

InputStream inputStream = null;

FileOutputStream fout = null;

BufferedReader br = null;

File fp = null;

double fileSize = 0;

int ofst = 0;

int num = 0;

StringWriter sw = new StringWriter();

boolean returnState = true;

try

{

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

{

resource = wdContext.currentContextElement().getFileResource();

strFileExtn = resource.getResourceType().getFileExtension().toUpperCase();

strFileName = resource.getResourceName();

// create an input stream to read the resource.

inputStream = resource.read(false);

// get the file size.

fileSize = inputStream.available();

// Create the byte array to hold the data

byte[] bytes = new byte[(int)fileSize];

// Read in the bytes

while (ofst < bytes.length && (num = inputStream.read(bytes, ofst, bytes.length-ofst)) >= 0)

{

ofst += num;

}

fp = new File(strFileName);

fout = new FileOutputStream(fp);

fout.write(bytes);

fout.flush();

fout.close();

br = new BufferedReader(new FileReader(fp));

try

{

String txt = null;

int index = 0;

while (( txt = br.readLine()) != null)

{

//Get the Index for the 'Tab'

index = txt.indexOf('\t');

wdComponentAPI.getMessageManager().reportSuccess("Field -"txt.substring(0, index) " Value -"+txt.substring(index,txt.ngth()));

}

}

catch (IOException ex)

{

}

}

catch(FileNotFoundException fe)

{

}

catch(IOException ioe)

{

}

catch(Exception e)

{

}

Regards,

Ajay

ranjit_mohanty
Explorer
0 Kudos

Hi Ajay,

I copy paste your code. But its giving error in this line

byte[] bytes = new byte(int)fileSize;

and it showing cannot convert int to byte.Please help

Thanks

Ranjit

Former Member
0 Kudos

Hi,

Use the file upload UI element. Create a button (for example "populate form"). Read the text file inside the action, and populate the view with corresponding data. If you search (even google), you'll find relevant threads. Hope this helps.

Thanks

Srinivas