Using a File Reference to Upload to ASP.netSeptember 10th, 2009
If you're looking for a quick way to upload files from Flex to an IIS server using ASP.net, then you might want to check this out (you'll even get the .net code!) A few quick notes before diving into the code; one of the problems with the FileReference object in ActionScript is that it doesn't seem to know how to read any response data from the webserver after the file has been successfully uploaded. In many cases you'd want to upload the file and then return the name, or id that the server used save it. This code sample will demonstrate a way around this, but if you absolutely must return data from the server then you'll want to look into using more complicated approaches with the URLLoader object. Finally, this code sends a query string variable called 'name' that will hold a timestamp of when the file upload occurs. We'll use this timestamp to rename the file when it's saved on the server. So this is the hack that allows us to avoid having to read data returned from the server. We'll determine the final file name in Flex and then send it up to the server along with the file. The server will then rename the file as specified when it does the save. This is a crude way to give some insurance that you won't get files with duplicate names that overwrite one another. Once the upload completes, we use the same time stamp to insert a record into the database, so that we know how to retreive the file at a later time (db code not included).
private var fr:FileReference; private var fileName:String;
private function browseForFile(evt:Event):void{ //as a query string... fileName = new Date().getTime().toString(); fr.upload(urlRequest); } } private function frComplete(evt:Event):void{ //now you can use the fileName var to insert into the db For the .net code I create a new web form that uses a code-behind page. In the declarative code (or the front page) I deleted everything except for the very first line (I left only the page declaration). Here's the code-behind page:
Imports System.IO 'item called 'name' attached to it, we'll use the value to rename our 'file... If Not Request.QueryString("name") Is Nothing Then Try 'whatever value it contains Else 'complete... End If 'the complete event... Response.Write("all done") End Sub 'here's a crude way to check the file extension, you might want to check 'the mime type instead... Function checkFileExtention(ByVal ext As String) As Boolean
End Class
|
![]() RECENT ARTICLES
Stack Error 1023 - My Strange Encounter With Error 1023
Passing Values to a Custom ItemRenderer Flex - Create a Dashboard That Allows Drag and Resize Using SharedObjects Strange Hack For Downloading With a FileReference Flex Checkbox TreeItemRenderer Flex - Dynamically Create Time Segments Flex - The Fastest Way to Transfer Data? MORE... |
