Skip to Content
avatar image
Former Member

uploading text file in webdynpro view

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • avatar image
    Former Member
    Jul 09, 2010 at 01:44 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 09, 2010 at 03:36 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      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

  • avatar image
    Former Member
    Jul 12, 2010 at 02:11 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 14, 2010 at 09:38 AM

    I got the answer and solved it.Thanks Ajay.

    Add comment
    10|10000 characters needed characters exceeded