[Checkins] SVN: z3c.widget/sandbox/src/z3c/widget/flashupload/resources/ bug for files > 30k

Manfred Schwendinger manfred.schwendiger at lovelysystems.com
Mon Oct 2 10:46:24 EDT 2006


Log message for revision 70485:
  bug for files > 30k

Changed:
  U   z3c.widget/sandbox/src/z3c/widget/flashupload/resources/Upload.as
  U   z3c.widget/sandbox/src/z3c/widget/flashupload/resources/upload.fla
  U   z3c.widget/sandbox/src/z3c/widget/flashupload/resources/upload.js

-=-
Modified: z3c.widget/sandbox/src/z3c/widget/flashupload/resources/Upload.as
===================================================================
--- z3c.widget/sandbox/src/z3c/widget/flashupload/resources/Upload.as	2006-10-02 14:21:37 UTC (rev 70484)
+++ z3c.widget/sandbox/src/z3c/widget/flashupload/resources/Upload.as	2006-10-02 14:46:22 UTC (rev 70485)
@@ -1 +1 @@
-/**
    Widget for uploading one or more Files to a Zope3 Server. 

    for autentication we need to get a ticket id befor sending the post request.
    
    step1: get the ticket:
    
    ./ticket.html/filename=myfile.jpg
    this returns us a target path:
    targeturl=http://...... 
    this is the target where we post the file
    
    step2: send file to the given url
    
    configuration:
    on _level0 we receive the information about where to get the ticket - inside
    the ticket we receive the url where to upload. 
    
    _level0.target_path -> where to get the ticket
    _level0.allowed_types -> space seperated files of allowed types. 
    
    _level0.allowed_types = "Images (all jpgs and pngs); jpg jpeg png gif|

*/


import flash.net.FileReferenceList;
import flash.net.FileReference;

class Upload extends MovieClip{
   
    
    private var info_mc:MovieClip;
        
    private var file_array:Array;
    private var fileref:FileReferenceList;
    private var file:FileReference; // currently active file for upload
    
    private var allowed_types:Array;
    private var debug_txt:TextField;
    
    private var load_vars:LoadVars;
    
    private var overall_total:Number;
    private var overall_loaded:Number;
    
    private var file_counter:Number;
    private var file_amount:Number;
	
	private var data_xml:XML;
    
	private var error_msg:String;
	
	private var js_command_queue:Array; // only one getURL can be called inside one frame. so lets make a queue to avoid troubles. 
	
    public function Upload(){
        
        Stage.scaleMode = "noScale";
        Stage.align = "LT";
        
		js_command_queue = new Array();
        overall_total = 0;
        overall_loaded = 0;
        file_counter = 0;
        log("initialize. target: "+_level0.target_path);
    
        // allow upload from the local host
        System.security.allowDomain("http://localhost/");
        
        fileref = new FileReferenceList();
        fileref.addListener(this);
                
        info_mc.progress_file_mc.bar_mc.gotoAndStop(1);
        info_mc.progress_overall_mc.bar_mc.gotoAndStop(1);
        // get allowed file types
        
        info_mc._visible = false;

        var margin:Number = 5;
        var width:Number = Stage.width - 2*margin;
        
        info_mc.progress_file_mc._width = width;
        info_mc.progress_overall_mc._width = width;
        
		info_mc.filenum_txt._x = width - info_mc.filenum_txt._width;
		info_mc.filename_txt._width = info_mc.filenum_txt._x - info_mc.filename_txt._x 
        info_mc.progress_file_txt._x = width - info_mc.progress_file_txt._width;
		info_mc.label_file_progress_txt._width = info_mc.progress_file_txt._x - info_mc.label_file_progress_txt._x;
        info_mc.progress_overall_txt._x = width - info_mc.progress_overall_txt._width;
		info_mc.label_total_progress_txt._width = info_mc.progress_overall_txt._x - info_mc.label_total_progress_txt._x
		
		var xml_url:String = _level0.base_path+"/@@vars.xml";
		
		data_xml = new XML();
		data_xml["ptr"] = this;
		data_xml.ignoreWhite = true;
		data_xml.onLoad = function(){ this.ptr.parseDataXML(this); }
		data_xml.load(xml_url);
		log("vars url: "+xml_url);
    }
	
	/**
		parses the language dependend strings. 
	*/
	private function parseDataXML(obj_xml:XML):Void{
		log("xml loaded: "+obj_xml.firstChild);
		var nodes = obj_xml.firstChild.childNodes;
		for (var i=0; i<nodes.length; i++){
			var nodeval:String = nodes[i].firstChild.nodeValue; 
			switch(nodes[i].attributes.name){
				case "file_progress" :
					info_mc.label_file_progress_txt.text = nodeval;
					break;
				case "overall_progress" :
					info_mc.label_total_progress_txt.text = nodeval;
					break;
				case "error" :
					error_msg = nodeval;
					break;
				default:
					log("error: unexpected attribute: "+nodes[i].attributes.name);
					break;
			}
		}
	}
	
	
    /**
        creates an array with all allowed types
        the data gets parsed from the variable of _level0.allowed_types
        
        var file:FileReference;

        var allTypes:Array = new Array();
        var imageTypes:Object = new Object();
        imageTypes.description = "Images (*.jpg, *.jpeg, *.gif, *.png)";
        imageTypes.extension = "*.jpg; *.jpeg; *.gif; *.png";
        allTypes.push(imageTypes);

        var textTypes:Object = new Object();
        textTypes.description = "Text Files (*.txt, *.rtf)";
        textTypes.extension = "*.txt;*.rtf";
        allTypes.push(textTypes);
        
        arr = [{description:'a description', extension:'*.jpg'}, .....]
    */
    public function saveAllowedTypes(arr:Array):Void{
        allowed_types = arr;
    }
    
    /**
        starts the browsing process for uploading files
    */
    public function browse():Void{
		js_command_queue.push("javascript:z3cFlashUploadDisableBrowseButton()");
		//getURL("javascript:z3cFlashUploadDisableBrowseButton()");		
        if (allowed_types.length>0){
            fileref.browse(allowed_types);
        }
        else{
            fileref.browse();
        }
    }

    /**
        starts the upload queue
    */
    public function onSelect(fileRefList:FileReferenceList):Void{
        file_array = fileRefList.fileList;
        log("uploadFiles: "+file_array.length);        
        
        for (var i=0; i<file_array.length; i++){
            overall_total+=file_array[i].size;
        }
        
        if (file_array.length>0){
            info_mc._visible = true;
            file_counter = 0;
            file_amount = file_array.length;
            loadNextTicket();
        }
    }
    
    /**
        loads the next upload ticket via loadvars
        the ticket holds some security information which 
        are required for the upload
    */
    public function loadNextTicket():Void{
        if (file_array.length == 0){
            log("all files uploaded.");
			// all files uploaded. Fire JavaScript Event
			js_command_queue.push("javascript:z3cFlashUploadOnUploadCompleteFEvent()");
			//getURL("javascript:z3cFlashUploadOnUploadCompleteFEvent()");
			
            return;
        }
        
        load_vars = new LoadVars();
        load_vars["ptr"] = this;
        load_vars.onData = function(d:String){
            this["ptr"].log("load data: "+d);
            this["ptr"].loadNextFile(d);
        }
        load_vars.load(_level0.target_path+"/@@ticket");
    
    }
    
    /**
        loads the next file inside the file_array
    */
    public function loadNextFile(ticket:String):Void{        
        file = FileReference(file_array.pop());
        
        var url:String = _level0.target_path+"?ticket="+ticket;
        
        log("uploadNextFile: "+url);
        
        file.addListener(this);
        file.upload(url);
    }
    
    public function log(msg:String):Void{
        trace(msg);
        _level0.debug_mc.debug_txt.text+=msg+"\n";
		_level0.debug_mc.debug_txt.scroll = _level0.debug_mc.debug_txt.maxscroll;
    }
    
    /**
        FileReference Listeners....
    */
    
    public function onProgress(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void{
        log("###############################");
        var percent:Number = Math.round(bytesLoaded/bytesTotal * 100);
		if (percent>100) percent=100; // for any strange reason, the totalbytes can be smaller then the loade bytes :)
        log("onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal+" = "+percent+"%");
        
        info_mc.progress_file_mc.bar_mc.gotoAndStop(percent);
        info_mc.progress_file_txt.text = percent+"%";
        
        var percent_complete:Number = Math.round((overall_loaded+bytesLoaded) / overall_total * 100);
        info_mc.progress_overall_mc.bar_mc.gotoAndStop(percent_complete);
        info_mc.progress_overall_txt.text = percent_complete+"%";
    }
    
    public function onComplete(file:FileReference):Void{
        log("file upload complete");
        
        overall_loaded+=file.size;
        this.loadNextTicket();
		
		// tell javascript that the file is uploaded. 
		//getURL("javascript:z3cFlashUploadOnFileCompleteFEvent('"+file.name+"')");
		js_command_queue.push("javascript:z3cFlashUploadOnFileCompleteFEvent('"+file.name+"')");
    }

    public function onCancel(file:FileReference):Void{
        log("onCancel");
		js_command_queue.push("javascript:z3cFlashUploadOnCancelFEvent()");
    }
    
    public function onOpen(file:FileReference):Void{
        log("onOpen: "+file.name+" : "+file.size);
        file_counter++;
        
        info_mc.filename_txt.text = file.name +" ("+Math.round(file.size/1024)+"kb)";
        info_mc.filenum_txt.text = file_counter+"/"+file_amount;
    }

    public function onHTTPError(file:FileReference):Void {
        log("onHTTPError: " + file.name);
		js_command_queue.push("javascript:z3cFlashUploadOnErrorFEvent('"+this.error_msg+"')");
    }

    public function onIOError(file:FileReference):Void {
        log("onIOError: " + file.name);
		js_command_queue.push("javascript:z3cFlashUploadOnErrorFEvent('"+this.error_msg+"')");		
    }
    
    public function onSecurityError(file:FileReference, errorString:String):Void {
        log("onSecurityError: " + file.name + " errorString: " + errorString);
		js_command_queue.push("javascript:z3cFlashUploadOnErrorFEvent('"+this.error_msg+"')");		
    }

    public function onEnterFrame():Void{
        if (_level0.startBrowse=="go"){
			log("go event was fired");
            _level0.startBrowse = "";
            this.browse();
        }
		
		if (js_command_queue.length>0){
			var command = js_command_queue.shift();
			getURL(command);
		}
		
    }
	
}
\ No newline at end of file
+/**
    Widget for uploading one or more Files to a Zope3 Server. 

    for autentication we need to get a ticket id befor sending the post request.
    
    step1: get the ticket:
    
    ./ticket.html/filename=myfile.jpg
    this returns us a target path:
    targeturl=http://...... 
    this is the target where we post the file
    
    step2: send file to the given url
    
    configuration:
    on _level0 we receive the information about where to get the ticket - inside
    the ticket we receive the url where to upload. 
    
    _level0.target_path -> where to get the ticket
    _level0.allowed_types -> space seperated files of allowed types. 
    
    _level0.allowed_types = "Images (all jpgs and pngs); jpg jpeg png gif|

    @author <manfred.schwendinger at lovelysystems.com>
*/


import flash.net.FileReferenceList;
import flash.net.FileReference;

class Upload extends MovieClip{
   
    
    private var info_mc:MovieClip;
        
    private var file_array:Array;
    private var fileref:FileReferenceList;
    private var file:FileReference; // currently active file for upload
    
    private var allowed_types:Array;
    private var debug_txt:TextField;
    
    private var load_vars:LoadVars;
    
    private var overall_total:Number;
    private var overall_loaded:Number;
    
    private var file_counter:Number;
    private var file_amount:Number;
	
	private var data_xml:XML;
    
	private var error_msg:String;
	
	private var js_command_queue:Array; // only one getURL can be called inside one frame. so lets make a queue to avoid troubles. 
	
    public function Upload(){
        
        Stage.scaleMode = "noScale";
        Stage.align = "LT";
        
		js_command_queue = new Array();
        overall_total = 0;
        overall_loaded = 0;
        file_counter = 0;
        log("initialize. target: "+_level0.target_path);
    
        // allow upload from the local host
        System.security.allowDomain("http://localhost/");
        
        fileref = new FileReferenceList();
        fileref.addListener(this);
                
        info_mc.progress_file_mc.bar_mc.gotoAndStop(1);
        info_mc.progress_overall_mc.bar_mc.gotoAndStop(1);
        // get allowed file types
        
        info_mc._visible = false;

        var margin:Number = 5;
        var width:Number = Stage.width - 2*margin;
        
        info_mc.progress_file_mc._width = width;
        info_mc.progress_overall_mc._width = width;
        
		info_mc.filenum_txt._x = width - info_mc.filenum_txt._width;
		info_mc.filename_txt._width = info_mc.filenum_txt._x - info_mc.filename_txt._x 
        info_mc.progress_file_txt._x = width - info_mc.progress_file_txt._width;
		info_mc.label_file_progress_txt._width = info_mc.progress_file_txt._x - info_mc.label_file_progress_txt._x;
        info_mc.progress_overall_txt._x = width - info_mc.progress_overall_txt._width;
		info_mc.label_total_progress_txt._width = info_mc.progress_overall_txt._x - info_mc.label_total_progress_txt._x
		
		var xml_url:String = _level0.base_path+"/@@vars.xml";
		
		data_xml = new XML();
		data_xml["ptr"] = this;
		data_xml.ignoreWhite = true;
		data_xml.onLoad = function(){ this.ptr.parseDataXML(this); }
		data_xml.load(xml_url);
		log("vars url: "+xml_url);
    }
	
	/**
		parses the language dependend strings. 
	*/
	private function parseDataXML(obj_xml:XML):Void{
		log("xml loaded: "+obj_xml.firstChild);
		var nodes = obj_xml.firstChild.childNodes;
		for (var i=0; i<nodes.length; i++){
			var nodeval:String = nodes[i].firstChild.nodeValue; 
			switch(nodes[i].attributes.name){
				case "file_progress" :
					info_mc.label_file_progress_txt.text = nodeval;
					break;
				case "overall_progress" :
					info_mc.label_total_progress_txt.text = nodeval;
					break;
				case "error" :
					error_msg = nodeval;
					break;
				default:
					log("error: unexpected attribute: "+nodes[i].attributes.name);
					break;
			}
		}
	}
	
	
    /**
        creates an array with all allowed types
        the data gets parsed from the variable of _level0.allowed_types
        
        var file:FileReference;

        var allTypes:Array = new Array();
        var imageTypes:Object = new Object();
        imageTypes.description = "Images (*.jpg, *.jpeg, *.gif, *.png)";
        imageTypes.extension = "*.jpg; *.jpeg; *.gif; *.png";
        allTypes.push(imageTypes);

        var textTypes:Object = new Object();
        textTypes.description = "Text Files (*.txt, *.rtf)";
        textTypes.extension = "*.txt;*.rtf";
        allTypes.push(textTypes);
        
        arr = [{description:'a description', extension:'*.jpg'}, .....]
    */
    public function saveAllowedTypes(arr:Array):Void{
        allowed_types = arr;
    }
    
    /**
        starts the browsing process for uploading files
    */
    public function browse():Void{
		js_command_queue.push("javascript:lovelyFlashUploadDisableBrowseButton()");
		//getURL("javascript:lovelyFlashUploadDisableBrowseButton()");		
        if (allowed_types.length>0){
            fileref.browse(allowed_types);
        }
        else{
            fileref.browse();
        }
    }

    /**
        starts the upload queue
    */
    public function onSelect(fileRefList:FileReferenceList):Void{
        file_array = fileRefList.fileList;
        log("uploadFiles: "+file_array.length);        
        
        for (var i=0; i<file_array.length; i++){
            overall_total+=file_array[i].size;
        }
        
        if (file_array.length>0){
            info_mc._visible = true;
            file_counter = 0;
            file_amount = file_array.length;
            loadNextTicket();
        }
    }
    
    /**
        loads the next upload ticket via loadvars
        the ticket holds some security information which 
        are required for the upload
    */
    public function loadNextTicket():Void{
        if (file_array.length == 0){
            log("all files uploaded.");
			// all files uploaded. Fire JavaScript Event
			js_command_queue.push("javascript:lovelyFlashUploadOnUploadCompleteFEvent()");
			//getURL("javascript:lovelyFlashUploadOnUploadCompleteFEvent()");
			
            return;
        }
        
        load_vars = new LoadVars();
        load_vars["ptr"] = this;
        load_vars.onData = function(d:String){
            this["ptr"].log("load data: "+d);
            this["ptr"].loadNextFile(d);
        }
        load_vars.load(_level0.target_path+"/@@ticket");
    
    }
    
    /**
        loads the next file inside the file_array
    */
    public function loadNextFile(ticket:String):Void{        
        file = FileReference(file_array.pop());
        
        var url:String = _level0.target_path+"?ticket="+ticket;
        
        log("uploadNextFile: "+url);
        
        file.addListener(this);
        file.upload(url);
    }
    
    public function log(msg:String):Void{
        return;
        trace(msg);
        _level0.debug_mc.debug_txt.text+=msg+"\n";
		_level0.debug_mc.debug_txt.scroll = _level0.debug_mc.debug_txt.maxscroll;
    }
    
    /**
        FileReference Listeners....
    */
    

    public function onProgress(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void{
        log("###############################");
        var percent:Number = Math.round(bytesLoaded/bytesTotal * 100);
		if (percent>100) percent=100; // for any strange reason, the totalbytes can be smaller then the loade bytes :)
        log("onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal+" = "+percent+"%");
        
        info_mc.progress_file_mc.bar_mc.gotoAndStop(percent);
        info_mc.progress_file_txt.text = percent+"%";
        
        var percent_complete:Number = Math.round((overall_loaded+bytesLoaded) / overall_total * 100);
        info_mc.progress_overall_mc.bar_mc.gotoAndStop(percent_complete);
        info_mc.progress_overall_txt.text = percent_complete+"%";
    }

    public function onComplete(file:FileReference):Void{
        log("file upload complete");
        
        overall_loaded+=file.size;
        this.loadNextTicket();
		
		// tell javascript that the file is uploaded. 
		//getURL("javascript:lovelyFlashUploadOnFileCompleteFEvent('"+file.name+"')");
		js_command_queue.push("javascript:lovelyFlashUploadOnFileCompleteFEvent('"+file.name+"')");
    }

    public function onCancel(file:FileReference):Void{
        log("onCancel");
		js_command_queue.push("javascript:lovelyFlashUploadOnCancelFEvent()");
    }
    
    public function onOpen(file:FileReference):Void{
        log("onOpen: "+file.name+" : "+file.size);
        file_counter++;
        
        info_mc.filename_txt.text = file.name +" ("+Math.round(file.size/1024)+"kb)";
        info_mc.filenum_txt.text = file_counter+"/"+file_amount;
    }

    public function onHTTPError(file:FileReference):Void {
        log("onHTTPError: " + file.name);
		js_command_queue.push("javascript:lovelyFlashUploadOnErrorFEvent('"+this.error_msg+"')");
    }

    public function onIOError(file:FileReference):Void {
        log("onIOError: " + file.name);
		js_command_queue.push("javascript:lovelyFlashUploadOnErrorFEvent('"+this.error_msg+"')");		
    }
    
    public function onSecurityError(file:FileReference, errorString:String):Void {
        log("onSecurityError: " + file.name + " errorString: " + errorString);
		js_command_queue.push("javascript:lovelyFlashUploadOnErrorFEvent('"+this.error_msg+"')");		
    }

    public function onEnterFrame():Void{
        if (_level0.startBrowse=="go"){
			log("go event was fired");
            _level0.startBrowse = "";
            this.browse();
        }
		
		if (js_command_queue.length>0){
			var command = js_command_queue.shift();
			getURL(command);
		}
		
    }
	
}
\ No newline at end of file

Modified: z3c.widget/sandbox/src/z3c/widget/flashupload/resources/upload.fla
===================================================================
(Binary files differ)

Modified: z3c.widget/sandbox/src/z3c/widget/flashupload/resources/upload.js
===================================================================
--- z3c.widget/sandbox/src/z3c/widget/flashupload/resources/upload.js	2006-10-02 14:21:37 UTC (rev 70484)
+++ z3c.widget/sandbox/src/z3c/widget/flashupload/resources/upload.js	2006-10-02 14:46:22 UTC (rev 70485)
@@ -1,4 +1,4 @@
-function z3cFlashUploadStartBrowsing(){
+function lovelyFlashUploadStartBrowsing(){
     // tells flash to start with browsing
     if(window.fuploader){
         window.document["fuploader"].SetVariable("startBrowse", "go");
@@ -7,40 +7,40 @@
     }         
 }
 
-function z3cFlashUploadDisableBrowseButton(){
+function lovelyFlashUploadDisableBrowseButton(){
     $("flash.start.browsing").style.visibility = "hidden";
     $("flash.start.browsing").disabled = "disabled";
 }
 
-function z3cFlashUploadOnUploadCompleteFEvent(status){
+function lovelyFlashUploadOnUploadCompleteFEvent(status){
     // always fired from flash
-    if (typeof(z3cFlashUploadOnUploadComplete) == "function"){
-        z3cFlashUploadOnUploadComplete(status);
+    if (typeof(lovelyFlashUploadOnUploadComplete) == "function"){
+        lovelyFlashUploadOnUploadComplete(status);
     }
 }
 
-function z3cFlashUploadOnFileCompleteFEvent(filename){
+function lovelyFlashUploadOnFileCompleteFEvent(filename){
     // always fired from flash
-    if (typeof(z3cFlashUploadOnFileComplete) =="function"){
-        z3cFlashUploadOnFileComplete(filename);
+    if (typeof(lovelyFlashUploadOnFileComplete) =="function"){
+        lovelyFlashUploadOnFileComplete(filename);
     }
 }
 
 /**
     called when the user presses the cancel button while browsing
 */  
-function z3cFlashUploadOnCancelFEvent(){
-    if (typeof(z3cFlashUploadOnCancelEvent) =="function"){
-        z3cFlashUploadOnCancelEvent(filename);
+function lovelyFlashUploadOnCancelFEvent(){
+    if (typeof(lovelyFlashUploadOnCancelEvent) =="function"){
+        lovelyFlashUploadOnCancelEvent(filename);
     }    
 }
 
 /**
     called if an error occured during the upload progress 
 */
-function z3cFlashUploadOnErrorFEvent(error_str){
-    if (typeof(z3cFlashUploadOnErrorEvent) =="function"){
-        z3cFlashUploadOnErrorEvent(error_str);
+function lovelyFlashUploadOnErrorFEvent(error_str){
+    if (typeof(lovelyFlashUploadOnErrorEvent) =="function"){
+        lovelyFlashUploadOnErrorEvent(error_str);
     }    
 }
 /**



More information about the Checkins mailing list