Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Contributor
koidy
Posts: 27
Registered: ‎04-05-2011
My Device: playbook
My Carrier: moblie app developer

can't download File with fileStream

 

import flash.events.Event;
	import flash.events.OutputProgressEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	import flash.utils.setTimeout;
	import qnx.display.*;
	import qnx.dialog.AlertDialog;
	import qnx.display.IowWindow;
	
	public class downloadFil2
	{
		
		private var req:URLRequest;
		private var stream:URLStream;
		private var fileName:String = "";
		private var file:File;
		
		public function downloadFil2()
		{
		}
		
		
		public function download(url){
			var temp:Array = url.split("/");
			fileName = temp[temp.length-1];
			
			var alert:AlertDialog = new AlertDialog();
			alert.message = fileName;
			alert.addButton("Yes");
			alert.show(IowWindow.getAirWindow().group);
			
			req = new URLRequest(url);
			stream = new URLStream();
			stream.addEventListener(Event.COMPLETE, writeAirFile);
			stream.load(req);
		}

		
		
		private function writeAirFile(evt:Event):void {
			var fileData:ByteArray = new ByteArray();
			stream.readBytes(fileData,0,stream.bytesAvailable);
			var file:File = File.documentsDirectory.resolvePath(fileName);
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeBytes(fileData,0,fileData.length);
			fileStream.close();
			if(file.exists){
				var alert:AlertDialog = new AlertDialog();
				alert.message = "exists";
				alert.addButton("Yes");
				alert.show(IowWindow.getAirWindow().group);
			}else{
				var alert:AlertDialog = new AlertDialog();
				alert.message = "not exists";
				alert.addButton("Yes");
				alert.show(IowWindow.getAirWindow().group);
			}
		}
	}

 

download file but not respond..

 

 

 

Please use plain text.
Contributor
koidy
Posts: 27
Registered: ‎04-05-2011
My Device: playbook
My Carrier: moblie app developer

Re: can't download File with fileStream

i have try another methods, it can download by
File.applicationStorageDirectory 

 

but if i use File.documentsDirectory, it can't download.

 

 

also when i use File.applicationStorageDirectory, then display with 

openWithDefaultApplication();

 

it have an error of "The file path is not valid"

 

 

package Common
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	import qnx.dialog.AlertDialog;
	import mx.controls.Alert;
	import qnx.display.IowWindow;
	import mx.events.CloseEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	public class downloadFile4
	{
		private var urlLoader:URLLoader = new URLLoader();
		private var fileName:String = "";
		
		public function downloadFile4(url:String)
		{
			var temp:Array = url.split("/");
			fileName = temp[temp.length-1];
			
			var alert:AlertDialog = new AlertDialog();
			alert.message = fileName;
			alert.addButton("Yes");
			alert.show(IowWindow.getAirWindow().group);
			
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.load(new URLRequest(url));
			urlLoader.addEventListener(Event.COMPLETE,tileLoaded);
		}
		protected function tileLoaded(event:Event):void {
			
			
			var tilefile:File = File.applicationStorageDirectory.resolvePath(fileName);
			if (tilefile.exists) {
				tilefile.deleteFile();
				var alert:AlertDialog = new AlertDialog();
				alert.message = "existsed";
				alert.addButton("Y");
				alert.show(IowWindow.getAirWindow().group);
			}else{
				var alert:AlertDialog = new AlertDialog();
				alert.message = "existsedno";
				alert.addButton("Yes");
				alert.show(IowWindow.getAirWindow().group);
			}
			var fileStream:FileStream = new FileStream();
			fileStream.open(tilefile, FileMode.WRITE);
			fileStream.writeBytes(urlLoader.data);
			fileStream.close();
			if(tilefile.exists){
				var alert:AlertDialog = new AlertDialog();
				alert.message = "exists";
				alert.addButton("Yes");
				alert.show(IowWindow.getAirWindow().group);
			}else{
				var alert:AlertDialog = new AlertDialog();
				alert.message = "not exists";
				alert.addButton("Yes");
				alert.show(IowWindow.getAirWindow().group);
			}
			tilefile.openWithDefaultApplication();
		}
	}
}

 

 

 

Please use plain text.
BlackBerry Development Advisor
elena_laskavaia
Posts: 417
Registered: ‎10-27-2010
My Device: PlayBook

Re: can't download File with fileStream

documents folder is shared directory, you would need to declare access_shared in the blackberry-tablet.xml (if you really intend to save to shared)
Please use plain text.
Contributor
koidy
Posts: 27
Registered: ‎04-05-2011
My Device: playbook
My Carrier: moblie app developer

Re: can't download File with fileStream

i am sure i have declare access_shared in the blackberry-tablet.xml , but still can save to share folder

here is my blackberry-table.xml:

 

 

<qnx>
	<initialWindow>
    <systemChrome>none</systemChrome>
    <transparent>true</transparent>
  </initialWindow>
	<publisher>XXXX</publisher>
    <author>XXXX</author>
     <authorId>XXXXXXXXXX</authorId>
    <action>use_camera</action>
    <action>read_geolocation</action>
    <action>play_audio</action>
    <action>access_internet</action>
    <action>access_shared</action>
    <icon>
   		 <image>blackberry-tablet-icon.png</image>
  	</icon>
  	
    <buildId>11</buildId>
    <platformVersion>1.0.0.0</platformVersion>
</qnx>

 

 

Please use plain text.
BlackBerry Development Advisor
elena_laskavaia
Posts: 417
Registered: ‎10-27-2010
My Device: PlayBook

Re: can't download File with fileStream

Does it work on simulator? What is the full path of file you trying to save? What is the return code from opening file?
Please use plain text.
Contributor
koidy
Posts: 27
Registered: ‎04-05-2011
My Device: playbook
My Carrier: moblie app developer

Re: can't download File with fileStream

it don't work in simulator

it has an error:

Error: Error #3001: File or directory access denied.

Please use plain text.