9 Replies Latest reply on Jul 28, 2010 1:53 AM by mfriasv

    Upload to ASP.NET

    susilosaja
      Hi ,
      I need to upload from Flex 2 to ASP.NET
      Can someone point me how to do this ?
      I dont mind using webservice or just an aspx , as long it's ASP.NET

      Searching in google only showed me sample of uploading from an aspx page , using HTMLInputFile, while I need to upload from Flex.

      Another example of uploading to a webservice showed me that the webservice accept byte array as the parameter , and I dont know how to get the byte array from a FileReference object.

      Regards,
      Susilo
        • 1. Re: Upload to ASP.NET
          babo_ya Level 3
          Try this.
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
          <![CDATA[


          public var fileRef:FileReference;

          public function startUpload():void
          {
          fileRef = new FileReference();
          fileRef.addEventListener(Event.SELECT, selectHandler);
          fileRef.addEventListener(Event.COMPLETE, completeHandler);

          try
          {
          var suc:Boolean = fileRef.browse();
          }
          catch(error:Error)
          {
          trace("Unable to browse for files.");
          }
          }
          public function selectHandler(event:Event):void
          {
          var request:URLRequest = new URLRequest(" http://www....");
          try
          {
          fileRef.upload(request);
          }
          catch (error:Error)
          {
          trace("Unable to upload file.");
          }
          }
          public function completeHandler(event:Event):void
          {
          trace("uploaded");
          }

          ]]>
          </mx:Script>



          <mx:Button label="Upload" click="startUpload();" />

          </mx:Application>
          • 2. Re: Upload to ASP.NET
            susilosaja Level 1
            Hi babo_ya,
            Thanks for the reply.

            But the thing is , if we're doing it this way in Flex , I dont know how to accept the upload in ASP.NET.

            Like I said above , the sample I saw on ASP.NET is that it's using a HtmlInputFile, which is an aspx component, and it can only be used for upload from aspx page, while I need to upload from Flex.

            This is one sample of using HtmlInputFile in ASP.NET :
            http://www.codeproject.com/aspnet/fileupload.asp

            Thanks,
            Susilo
            • 3. Re: Upload to ASP.NET
              susilosaja Level 1
              Ah ok found it.
              Turns out to be quite simple.

              Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              Dim file As HttpPostedFile = Request.Files(0)
              file.SaveAs(....)
              End Sub
              • 4. Re: Upload to ASP.NET
                jlingwai Level 1
                Hey,
                I have my flex uploading in asp.net but I'm trying to change the file name when it uploads....
                How are you accessing your Sub from flex? with a POST??

                -JoSh
                • 5. Re: Upload to ASP.NET
                  susilosaja Level 1
                  I'm using FileReference object, which means it use POST to upload.
                  I dont see how you can change the name of the file being browse.
                  Although you might can save it as a different name on the server.

                  Susilo
                  • 6. Re: Upload to ASP.NET
                    jlingwai Level 1
                    Yea thats what I'm trying to do....But I can't seem to send the new file name to the the POST or the server script. Do you have any Idea how to accomplish this?

                    Thanks
                    JoSh
                    • 7. Re: Upload to ASP.NET
                      susilosaja Level 1
                      You could use query string on the url :

                      On the Flex code :

                      var url:String = " http://www.server.com/upload.aspx?filename=xxx.txt";
                      file.upload(new URLRequest(url));

                      And on the ASP.NET code :

                      Dim filename As String = Request.Params("filename")


                      Cheers,
                      Susilo
                      • 8. Re: Upload to ASP.NET
                        jlingwai Level 1
                        Susilo,

                        Thanks that worked perfectly

                        -JoSh
                        • 9. Re: Upload to ASP.NET
                          mfriasv

                          Hi Susilo,

                           

                          I am trying to accomplish the same thing without success so far.

                           

                          I have a FileReference.upload in my app and now I don't know how the ASP script should look like. I tried your example but, to begin with, MyBase.Load is not accepted.

                           

                          Then I don't know how does your code accept a parameter. Do you have a Web Service as in my case? I wrote this but I don't know if it's right:

                           

                          Public Function importFile(ByVal f As Byte(), ByVal fileName As String) As String
                                  Try
                                      Dim ms As New MemoryStream(f)
                                      Dim fs As New FileStream(System.Web.Hosting.HostingEnvironment.MapPath("C:\temp") & fileName, FileMode.Create)
                                      ms.WriteTo(fs)
                                      ms.Close()
                                      fs.Close()
                                      fs.Dispose()
                                      Return "ok"

                           

                                  Catch ex As Exception
                                      Return ex.Message.ToString()
                                  End Try

                           

                              End Function

                           

                          Thanks in advance,

                          Manolo