cancel
Showing results for 
Search instead for 
Did you mean: 

How to read the contents of the Uploaded file ?

Former Member
0 Kudos

hi @,

I have used the File upload and Download UI elements as per the tutorial available. Now my requirement is to read the contents of the file and transfer it to the Backend system.

How can I achieve the desired fucntionality.

Thanks in advance,

Regards,

Amit

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

try this code to store image file in back end system.

IWDResource res=wdContext.currentMyDataElement().getPictureres();
		InputStream in=res.read(false);
		ByteArrayOutputStream bout=new ByteArrayOutputStream();
		int length;
		byte[] part=new byte[10*1024];
		while((length=in.read(part))!=-1)
		{
		bout.write(part,0,length);
		}
		in.close();
pstmt.setBytes(8,bout.toByteArray());

If it is other than image file like word or text then try this in action

IWDResource resource = wdContext.currentResElement().getResorce(); // your existing handle to the upload resource type
		  try {
			InputStream stream = resource.read(true);
			byte b[]= new byte[1000];
			stream.read(b);
			String str = new String(b);
			int i=str.length();
			wdContext.currentContextElement().setOut(str);
			wdContext.currentContextElement().setSize(i);
			wdContext.currentResElement().setResourceurl(
				wdContext.currentResElement().getResorce().getUrl(WDFileDownloadBehaviour.OPEN_INPLACE.ordinal()));
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

The in back end field where u store the file should be byte array.

Regards,

H.V.Swathi

Answers (1)

Answers (1)

p_2_5_6_9_6_0
Active Participant
0 Kudos

Hi,

I am not sure what kind of file you are reading - most probably a .csv or text file - in either case the following source code may help you.

String[24][24] numbers; //use an ArrayList here in case you dont know the number of columns or row.
 
File file = new File("something.csv"); //use the mimes path here
 
BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
 
//read each line of text file
while((line = bufRdr.readLine()) != null)
{
	StringTokenizer st = new StringTokenizer(line,",");
	while (st.hasMoreTokens())
	{
		//get next token and store it in the array
		numbers[row][col] = st.nextToken();
		col++;
	}
	row++;
}
 
//close the file
bufRdr.close();

The read contents will be stored in the ArrayList or the String Array. Use the contents of the array to call a BAPI (via Models) and insert the data into the backend system.

Hope that helps.

Thanks.

p256960.