{"id":530,"date":"2009-06-16T18:29:54","date_gmt":"2009-06-17T00:29:54","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/archives\/000530"},"modified":"2009-06-16T18:32:59","modified_gmt":"2009-06-17T00:32:59","slug":"sending-and-receiving-more-than-a-file-with-flex-filereference","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/530","title":{"rendered":"Sending (and receiving) more than a file with Flex FileReference"},"content":{"rendered":"<p>The <a href=\"http:\/\/livedocs.adobe.com\/flex\/3\/langref\/flash\/net\/FileReference.html\">Flex FileReference object<\/a> makes it very easy to send a file to the server.\u00a0 I had a situation where I wanted to send some additional data and also get back server output.\u00a0 This is possible, but not entirely intuitive, so I wanted to document this for others.\u00a0 In the process of making this work, <a href=\"http:\/\/blog.flexexamples.com\/2007\/09\/21\/uploading-files-in-flex-using-the-filereference-class\/\">this post<\/a> and <a href=\"http:\/\/blog.flexexamples.com\/2007\/10\/30\/using-the-urlvariables-and-filereference-classes-to-pass-data-from-flex-to-a-server-side-script\/\">this post<\/a> were very helpful to me.<\/p>\n<p>To send more than a file, you use a <a href=\"http:\/\/livedocs.adobe.com\/flex\/2\/langref\/flash\/net\/URLVariables.html\">URLVariables object<\/a>, like you normally would.\u00a0 The key is to realize that you also have to set the <code>URLRequest.method<\/code> to <code>URLRequestMethod.POST<\/code>, otherwise these variables get lost.\u00a0 (Makes sense&#8211;no one sends files via GET, but it was not obvious to me.)<\/p>\n<pre>var request:URLRequest = new URLRequest(url);\r\nvar variables:URLVariables = new URLVariables();\r\nvariables.docname = docName.text;\r\nrequest.data = variables;\r\nrequest.method = URLRequestMethod.POST;\r\ntry {\r\nfileRef.upload(request);\r\n} catch (error:Error) {\r\ntrace(\"Unable to upload file.\");\r\n}<\/pre>\n<p>To get any response from the server (like a success message or filename), you have to attach a listener to the <a href=\"http:\/\/livedocs.adobe.com\/flex\/3\/langref\/flash\/net\/FileReference.html#event:uploadCompleteData\"><code>DataEvent.UPLOAD_COMPLETE_DATA<\/code> event<\/a>, like so:<\/p>\n<pre>fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadCompleteData);\r\n\r\n...\r\nprivate function onUploadCompleteData (event : DataEvent) : void {\r\nvar myData = new String(event.data);\r\n\/\/ do something with your serverside data...\r\n}<\/pre>\n<p>[tags]flex,file upload[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Flex FileReference object makes it very easy to send a file to the server.\u00a0 I had a situation where I wanted to send some additional data and also get [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,36],"tags":[],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-programming","category-ria"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=530"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/530\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}