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

    Uploading csv files and reading them from server

    ived
      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">
      <mx:Script>
      <![CDATA[
      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);
      fileRef.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
      try{
      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);
      }
      catch(error:Error){
      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");
      try
      {
      fileRef.upload(request);
      }
      catch (error:Error)
      {
      trace("Unable to upload file.");
      }
      }

      private function completeHandler(event:Event):void{
      trace("uploaded");
      }

      // 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:Script>
      <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>

      </mx:Application>

      2)struts.xml file

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

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

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

      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);



      @Override
      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>();
      try{
      BufferedReader reader = new BufferedReader(new FileReader(upload));
      String line =null;
      String symbol=null;
      while((line=reader.readLine())!=null){
      StringTokenizer tokenizer = new StringTokenizer(line,"\t");
      symbol = tokenizer.nextToken();
      if(symbol!=null) symbol = symbol.trim();
      if(symbol.length()>0)
      symbolList.add(symbol);
      }
      }catch(FileNotFoundException fne){
      if(logger.isDebugEnabled())
      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;
      }
      }