I am using flex 3, actionscript 3 and rails for developing a small
application. I want to upload a file on server in a particular
directory but i am getting the IO error at run time. Please help me
fixing this bug.
Code i used is:
---------------------------application.rb------------------------
Filters added to this controller apply to all controllers in the
application.
Likewise, all the methods added will be available for all
controllers.
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
See ActionController::RequestForgeryProtection for details
Uncomment the :secret if you’re not using the cookie session store
protect_from_forgery # :secret => ‘3a8f77511f4633ed7c0cb6065d614fec’
def upload_file
file_data = params[:Filedata]
file_name = params[:Filename].to_s
file_path = params[:folder].to_s
File.open(file_path + "/" + file_name, "wb") { |new_file|
new_file.write(file_data.read) }
render(:xml => “”)
end
end
------------------------PdfStore.mxml----------------
<?xml version="1.0" encoding="utf-8"?><mx:Application
xmlns:mx=“http://www.adobe.com/2006/mxml”
xmlns:pom=“com.PdfStore.components.*”
layout=“vertical”
backgroundGradientColors=“[#ffffff, #c0c0c0]”
horizontalAlign=“center”
verticalAlign=“top”
width=“100%”
height=“100%”>
<mx:Accordion width=“463” height=“300”>
pom:AvailablePdfs/
pom:UploadPdf/
</mx:Accordion>
</mx:Application>
----------------------------------UploadPdf.mxml---------------------
<?xml version="1.0" encoding="utf-8"?><mx:HBox xmlns:mx=“http://www.adobe.com/2006/mxml” width=“100%”
height=“100%” label=“Upload Pdf”>
mx:Script
<![CDATA[
/*function openf():void {
var fileTypes:FileFilter = new FileFilter("Pdf (*.pdf)", "*.pdf;
");
var allTypes:Array = new Array(fileTypes);
var fileRef:FileReference = new FileReference();
//fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.browse(allTypes);*/
import mx.rpc.http.HTTPService;
import mx.collections.ArrayCollection;
import flash.net.FileReferenceList;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
private static const fileFolder:String =
“myUploads”;//com/PdfStore/
assets";// //this folder is located in rails_file_transfer\public\
[Bindable]
private var filesToUpload:ArrayCollection = new ArrayCollection();
[Bindable]
private var uploadedFiles:ArrayCollection = new ArrayCollection();
//include "scripts/uploadScript.as";
]]>
</mx:Script>
<mx:Script source=“uploadScript.as”/>
<mx:Form height=“217” width=“499”>
<mx:FormItem required=“true” label=“Select File”>
<mx:VBox height=“130”>
<mx:TextInput id=“pdflocationTI” width=“250”/>
<mx:List x=“0” y=“0” width=“100%” height=“100%” id=“localFiles”
dataProvider=“{filesToUpload}”></mx:List>
<!--mx:Button id="browseButton" label="Browse" click="openf()"/-->
</mx:VBox>
</mx:FormItem>
<mx:FormItem width="349" height="64">
<mx:Button id="uploadButton" label="Upload" width="68"
click=“uploadFiles()”/>
<mx:ProgressBar labelPlacement=“center” width=“251”
height=“21”
id=“pb” mode=“manual” direction=“right”/>
</mx:FormItem>
</mx:Form>
</mx:HBox>
uploadScript.as--------------------------
// ActionScript file
private var uploadFileRefList:FileReferenceList = new
FileReferenceList();
private var uploadURL:URLRequest = new URLRequest(“http://localhost:
3000/application/upload_file”);
private function uploadFiles():void
{
uploadFileRefList.addEventListener(Event.SELECT,
uploadSelectHandler);
try {
var success:Boolean = uploadFileRefList.browse();
} catch (error:Error) {
trace("Unable to browse for files. " + error);
}
}
private function doUpload(file:FileReference):void
{
file.addEventListener(Event.COMPLETE, uploadCompleteHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, uploadIoErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS,
uploadProgressHandler);
var variables:URLVariables = new URLVariables();
variables.folder = "public/" + fileFolder;//bin/
uploadURL.data = variables;
try {
file.upload(uploadURL);
} catch (error:Error) {
trace("Unable to upload file. " + error);
}
}
private function uploadSelectHandler(event:Event):void
{
for each (var file:FileReference in uploadFileRefList.fileList)
filesToUpload.addItem(file.name);
doUpload(uploadFileRefList.fileList[0]);
}
private function uploadProgressHandler (event:ProgressEvent):void
{
pb.label = "Uploading " + event.currentTarget.name + " " +
Math.floor((event.bytesLoaded/event.bytesTotal)*100) + “% complete”;
pb.setProgress(event.bytesLoaded, event.bytesTotal);
}
private function uploadIoErrorHandler (event:Event):void
{
trace ("Upload failed: " + event);
}
private function uploadCompleteHandler (event:Event):void
{
uploadedFiles.addItem(event.currentTarget.name);
filesToUpload.removeItemAt(0);
uploadFileRefList.fileList.splice(0, 1);
if (filesToUpload.length == 0)
{
pb.label = "LOADING 0%";
pb.setProgress(0, 0);
event.currentTarget.removeEventListener(Event.COMPLETE,
uploadCompleteHandler);
event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,
uploadIoErrorHandler);
event.currentTarget.removeEventListener(ProgressEvent.PROGRESS,
uploadProgressHandler);
}
else
{
doUpload(uploadFileRefList.fileList[0]);
}
}