4 Replies Latest reply on Dec 26, 2007 10:12 PM by ived

    Uploading csv files and reading them from server

      I want to read a csv file.From Flex i am able to select the file but when i pass it to the server using struts FileUploadInterceptor , am not able to pass the file to the server.FileUploadInterceptor in struts2 processes the request only if its instance of MultiPartRequestWrapper.Is there any way in Flex where i can pass the request as a instance of this.Is there any other way in which i can read the file from the server after uploading it through flex.Code is as follows :

      1)MXML File :

      ?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      import ImportData;
      import flash.net.FileReference;
      import flash.net.FileFilter;
      import flash.events.IOErrorEvent;

      [Bindable] var fileRef:FileReference = new FileReference();
      private function openFileDialog():void{
      fileRef.addEventListener(Event.SELECT, selectHandler);
      fileRef.addEventListener(Event.COMPLETE, completeHandler);
      fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA ,uploadCompleteHandler);
      var textTypes:FileFilter = new FileFilter("Text Files (*.txt,*.csv)","*.txt;*.csv");
      var allTypes:Array = new Array(textTypes);
      //var success:Boolean = fileRef.browse();
      var success:Boolean = fileRef.browse(allTypes);
      trace("Unable to browse for files.");

      private function onIOError(event:IOErrorEvent):void {
      trace("In here"+event.text);
      trace("In here"+event.toString());
      // when a file is selected you upload the file to the upload script on the server
      private function selectHandler(event:Event):void{
      //var request:URLRequest = new URLRequest("/importAction");
      var request:URLRequest = new URLRequest(" http://localhost:8080/pack1/importAction.action");
      catch (error:Error)
      trace("Unable to upload file.");

      private function completeHandler(event:Event):void{

      // dispatched when file has been uploaded to the server script and a response is returned from the server
      // event.data contains the response returned by your server script

      public function uploadCompleteHandler(event:DataEvent):void {
      trace("uploaded... response from server: \n" + String(event.data));
      <mx:Button label="Import" id="importBtn" click="openFileDialog()" height="20" width="90" styleName="buttonsOnSearchBar"/>
      <mx:ComboBox x="23" y="44" borderColor="#ff0000" themeColor="#ff0000"></mx:ComboBox>


      2)struts.xml file

      <package name="pack1" extends="struts-default,json-default">
      <result name="error" type="json"></result>

      <exception-mapping result="error" exception="java.lang.Throwable"/>

      <action name="importAction" class="routing.ImportAction">
      <interceptor-ref name="fileUpload"/>
      <interceptor-ref name="basicStack"/>
      <result name="success" type="json"></result>

      3)Action Class

      package com.om.dh.orderrouting.action;

      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.util.ArrayList;
      import java.util.StringTokenizer;
      import org.apache.log4j.Logger;

      import com.opensymphony.xwork2.ActionSupport;

      public class ImportAction extends ActionSupport{

      private String contentType;
      private File upload;
      private String fileName;
      private String caption;

      private static final Logger logger = Logger.getLogger(ImportAction.class);

      public String execute() throws Exception {

      * Read File Line by Line.. If the file has more than one word separated by comma
      * return error.
      ArrayList<String> symbolList = new ArrayList<String>();
      BufferedReader reader = new BufferedReader(new FileReader(upload));
      String line =null;
      String symbol=null;
      StringTokenizer tokenizer = new StringTokenizer(line,"\t");
      symbol = tokenizer.nextToken();
      if(symbol!=null) symbol = symbol.trim();
      }catch(FileNotFoundException fne){
      logger.debug("File NotFount ", fne);

      for(String symbol1:symbolList)
      System.out.print(symbol1+" ");
      return SUCCESS;

      public String getUploadFileName() {
      return fileName;
      public void setUploadFileName(String fileName) {
      this.fileName = fileName;

      public String getUploadContentType() {
      return contentType;
      public void setUploadContentType(String contentType) {
      this.contentType = contentType;

      public File getUpload() {
      return upload;
      public void setUpload(File upload) {
      this.upload = upload;

      public String getCaption() {
      return caption;
      public void setCaption(String caption) {
      this.caption = caption;

      public String input() throws Exception {
      return SUCCESS;

      public String upload() throws Exception {
      return SUCCESS;