2 Replies Latest reply on Aug 3, 2010 4:02 AM by mfriasv

    Upload file to SQL Server from Flex

    mfriasv Level 1

      Hi,

       

      from my Flex app I want users to upload a .csv file to SQL server.I thought a possible approach would be:

       

      -upload file to the server

      -check the file there

      -send back the file to flex to sow it in a datagrid

       

      I am stuck in the uploading. I have the following code but I am always getting the 2038 i/o error. There is even no request to the server according to Httpfox. Any ideas will be appreciated.

       

      Flex:

       

      <mx:Canvas width="100%" height="100%" backgroundColor="#91A7B2">
              <mx:Script>
                  <![CDATA[
                      import mx.managers.PopUpManager;
                      import mx.controls.Alert;
                     
                      [Bindable]
                      private var fileRef:FileReference = new FileReference();
                      [Bindable]
                      private var fileSelected:Boolean = false;
                      [Bindable]
                      private var statusFile:String = "Select file";
                     
                      private var urlReq:URLRequest = new URLRequest();

       

                      private function onCreationComplete():void
                      {
                          fileRef = new FileReference();
                          fileRef.addEventListener(Event.SELECT, onFileSelected);
                          fileRef.addEventListener(Event.COMPLETE, onUploadComplete);
                          fileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
                          fileRef.addEventListener(IOErrorEvent.IO_ERROR, onUploadError);
                          fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadError);
                      }
                 
                      private function closeMe():void
                      {
                          PopUpManager.removePopUp(this);
                      }
                      private function selectFile(evt:MouseEvent):void
                      {
                          fileRef.browse();
                      }
                      private function onFileSelected(evt:Event):void
                      {
                          fileSelected = true;
                          fileInput.text = fileRef.name;
                          statusFile = "Ready";
                      }
                      private function uploadFile(event:MouseEvent):void
                      {
                          if (!fileSelected)
                          Alert.show("Select a file!");
                          else
                          urlReq.url = "http://myServer/website/CoreSetUploadFile/Default.aspx";
                          fileRef.upload(urlReq,"test");
                      }
                      private function onUploadProgress(event:ProgressEvent):void
                      {
                          statusFile = ((event.bytesLoaded * 100 / event.bytesTotal).toString());
                      }
                      private function onUploadComplete(event:DataEvent):void
                      {
                          statusFile = "Complete";
                      }
                      private function onUploadError(event:Event):void
                      {
                          if (event is IOErrorEvent)
                          {
                              Alert.show((event as IOErrorEvent).toString());
                          }
                          else if (event is SecurityErrorEvent)
                          {
                              Alert.show((event as SecurityErrorEvent).toString());
                          }
                      }
                     
                  ]]>
              </mx:Script>

       

              <mx:VBox x="52" y="70">
                  <mx:HBox>
                      <mx:Button label="Browse" click="selectFile(event)"/>
                      <mx:TextInput id="fileInput" editable="false" width="200"/>
                  </mx:HBox>
                  <mx:Button label="Upload file" click="uploadFile(event)" enabled="{fileSelected}"/>
                  <mx:HBox>
                      <mx:Text text="Status: "/>
                      <mx:Text text="{statusFile}"/>
                  </mx:HBox>
              </mx:VBox>
          </mx:Canvas>

       

       

       

      ASP.NET code:

       

         Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
              Dim uploadFile As HttpPostedFile = Request.Files("tts")
              uploadFile.SaveAs("test")

       

          End Sub

       

      Regards,

      Manolo