• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

write the contents that we read from a file to a textbox.

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We want to write the contents that we read from a file to a textbox.

Can anyone please share how to do this.

This is how we are reading the file
public ActionForward doUpload(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response) throws Exception{
ExampleForm csForm = (ExampleForm) form;

// Process the FormFile
FormFile myFile = csForm.getMyFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();

System.out.println("contentType: " + contentType);
System.out.println("File Name: " + fileName);
System.out.println("File Size: " + fileSize);
System.out.println("File data: " + fileData);

return mapping.findForward("success");
}

User clicks on the Browse button(implemented using html:file tag) to select the fileName and then clicks on Upload button.
This calls the doUpload method that I mentioned above. After this we need to write the contents of the file to a text box shown below the Upload button on the jsp.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add "fileData" to the request as an attribute. Use JSP expression syntax (<%= ... %> to get the data filled in textfield.

OR

You can make a bean with the file information (fileName, fileSize, fileData etc..) and set that bean in request and use syntax like:<jsp:getProperty name="fileBean" property="fileData"/>

Or you can make javascript variable in the action to set the data in the field.

Hope there more better ways. Its pretty simple I guess.

-Ratheesh
 
Vani Bandargal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I figured it out that I was not convering Byte to char. Very silly mistake.

Here is what we did

public ActionForward doUpload(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response) throws Exception{
try {
ExampleForm csForm = (ExampleForm ) form;

// Process the FormFile
FormFile myFile = csForm.getMyFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
StringBuffer fileContent = new StringBuffer();

char chars[] = new char[fileData.length];

for (int x = 0; x < fileData.length; x++) {
chars[x] = (char) fileData[x];
fileContent.append(chars[x]);
}




csForm.setUploadFileData(fileContent.toString());

request.setAttribute(mapping.getAttribute(), csForm);

} catch (Exception e) {
System.out.println("Exception: " + e);
logger.error("Exception: "+ e);
throw new WebException(e);
}
return mapping.findForward(IConstants.SUCCESS_KEY);
}

You are correct. as you mentioned we took that to a form variable and the textarea in the jsp is assigned that property.
[ August 01, 2008: Message edited by: Vani D Bandargal ]
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic