1 Reply Latest reply on Nov 13, 2009 3:21 AM by mlindhout79

    getting extra things in the uploaded file by flex file upload

    Naseem_Khan
      Hi,

      I am working on a application where I am using adobe flex. I have a requirement wher I have to upload file to the server. System allows user to browse file from local machine and flex uploads it to the server.

      Now there is a upload url, its a jsp page which gets mutipart/form-data content from the request, opens an outputstream and writes the content of the inputstrem to the file.


      fileUpload.mxml
      quote:


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="initApp()">
      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.utils.ObjectUtil;
      import flash.events.*;
      import flash.net.FileReference;
      import flash.net.URLRequest;


      private var fileRef:FileReference;

      private function initApp():void {
      fileRef = new FileReference();
      fileRef.addEventListener(Event.CANCEL, traceEvent);
      fileRef.addEventListener(Event.COMPLETE, completeEvent);
      fileRef.addEventListener(Event.SELECT, selectEvent);
      fileRef.addEventListener(IOErrorEvent.IO_ERROR, traceEvent);
      fileRef.addEventListener(Event.OPEN, traceEvent);
      fileRef.addEventListener(ProgressEvent.PROGRESS, progressEvent);
      fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, traceEvent);
      }

      private function traceEvent(event:Event):void {
      var tmp:String = "================================\n";
      //ta.text += tmp + event.type + " event:" + mx.utils.ObjectUtil.toString(event) + "\n" ;
      //ta.verticalScrollPosition += 20;
      }

      private function ioErrorEvent(event:IOErrorEvent):void{
      Alert.show("IOError:" + event.text);
      traceEvent(event);
      }

      private function selectEvent(event:Event):void{
      btn_upload.enabled = true;
      traceEvent(event);
      filename.text = fileRef.name;
      progressBar.setProgress(0, 100);
      progressBar.label = "Loading 0%";
      }

      private function progressEvent(event:ProgressEvent):void {
      progressBar.setProgress(event.bytesLoaded, event.bytesTotal);
      traceEvent(event);
      }

      private function completeEvent(event:Event):void {
      progressBar.label = "Complete.";
      filename.text += " uploaded";
      traceEvent(event);
      btn_upload.enabled = false;
      btn_cancel.enabled = false;
      }

      private function uploadFile(endpoint:String):void {
      var param:String = "author=" + ti_author.text;
      var req:URLRequest = new URLRequest(endpoint);
      req.method = URLRequestMethod.POST;
      fileRef.upload(req, param, false);
      progressBar.label = "Uploading...";
      btn_cancel.enabled = true;
      }

      ]]>
      </mx:Script>

      <mx:Panel title="Flex 2 File Uploading Demo" width="100%" height="100%" >
      <mx:Form>

      <mx:FormItem label="Upload URL:">
      <mx:TextInput editable="false" id="uploadURL" width="100%" text=" http://localhost/flexApp/jsps/upload.jsp" enabled="true" />
      </mx:FormItem>

      <mx:FormItem label="Selected File:">
      <mx:Label id="filename"/>
      </mx:FormItem>

      <mx:FormItem label="Upload By:">
      <mx:TextInput id="ti_author" text="Author" />
      </mx:FormItem>

      <mx:FormItem direction="horizontal" width="100%">
      <mx:Button width="80" label="Browse" click="fileRef.browse()" />
      <mx:Button width="80" label="Upload" id="btn_upload" enabled="false" click="uploadFile(uploadURL.text)" />
      <mx:Button width="80" label="Cancel" id="btn_cancel" enabled="false" click="fileRef.cancel()" />
      </mx:FormItem>

      <mx:HRule width="100%" tabEnabled="false"/>

      <mx:FormItem label="Progress:">
      <mx:ProgressBar id="progressBar" mode="manual" />
      </mx:FormItem>

      </mx:Form>

      </mx:Panel>
      </mx:Application>




      test.jsp page
      quote:


      InputStream is = null;
      BufferedInputStream bis = null;
      BufferedOutputStream bos = null;
      try {
      System.out.println("request.getContentType()...." +
      request.getContentType());

      is = request.getInputStream();

      bis = new BufferedInputStream(is);

      bos = new BufferedOutputStream(new FileOutputStream(new File("d:/naseem.txt")));

      int i=0;
      while ((i = bis.read())!=-1) {
      bos.write(i);
      }
      bos.flush();

      }
      catch(Exception e){
      e.printStackTrace();
      }
      finally{
      try{
      if(bos!=null && is!=null && bis!=null){
      bos.close();is.close();bis.close();
      }
      }catch(Exception e){e.printStackTrace();}
      }





      Above jsp writes the content of browsed file to d:/naseem.txt

      Problem which I am facing is besides the actual content, it also write something else.


      ----------------------
      Actual file:

      quote:


      My Text




      ----------------------
      upload txt file which I got: naseem.txt

      quote:


      ------------cH2ei4GI3ae0KM7cH2GI3ei4ae0cH2
      Content-Disposition: form-data; name="Filename"

      test.txt
      ------------cH2ei4GI3ae0KM7cH2GI3ei4ae0cH2
      Content-Disposition: form-data; name="author=Author"; filename="test.txt"
      Content-Type: application/octet-stream

      My Text
      ------------cH2ei4GI3ae0KM7cH2GI3ei4ae0cH2
      Content-Disposition: form-data; name="Upload"

      Submit Query
      ------------cH2ei4GI3ae0KM7cH2GI3ei4ae0cH2--




      My question is how do I remove these content-disposition, content type i.e., extra things from my file. I need only content to be written in the file.

      Regards,
      Naseem