Skip to Content
avatar image
Former Member

How to read the contents of the Uploaded file ?

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Dec 29, 2008 at 05:30 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Dec 29, 2008 at 05:04 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded