Hello, can someone help me?
I want to download files from the server side to client side without prompting a window to the user to download when any updates happen at server side.
Right now I am using urlstream class but first file is downloading completely rest of the files contents downloading partially.
edit
Code sample taken from other post. Warning: it's a huuuge chunk o'code.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="client.components.*" layout="absolute" height="554" width="817"
backgroundGradientAlphas="[1.0, 0.0]"
backgroundGradientColors="[#ABA9A7, #03143B]" creationComplete="init();">
<mx:Script>
<![CDATA[
import flash.system.Capabilities;
import mx.collections.XMLListCollection;
import mx.rpc.Fault;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.events.ListEvent;
import mx.collections.ArrayCollection;
import flash.data.SQLConnection;
import flash.errors.SQLError;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;
import flash.errors.SQLError;
import mx.controls.Alert;
import mx.events.CloseEvent;
import flash.net.*;
import flash.filesystem.*;
import flash.events.OutputProgressEvent;
private var urlstring:String="server path";
private var urlReq:URLRequest;
private var urlStream:URLStream;
private var fileData:ByteArray = new ByteArray();
private var sqlConnection:SQLConnection =null;
private var sql:String;
private var result:SQLResult;
private var stmt:SQLStatement=null;
private var catid:int=0;
private var testcollection:Array=new Array();
[Bindable]
private var DGArray:ArrayCollection = new ArrayCollection(testcollection);
private var number:int;
private var selection:Boolean=false;
private var timestamp:String;
private var xmlcol:XMLListCollection;
private var categoryid:int=0;
private var numbers:int;
private var index:int=0;
private var findex:int=0;
private var nupdates:int=0;
private var newfile:String="";
private var selectstream:int=1;
private var startdownload:Boolean=false;
public function init():void{
userRequest.send(null);
sqlConnection= new SQLConnection();
//create a new db-file in the application storage location
var file:File = new File("app:/E-catalog.db");
sqlConnection.addEventListener(SQLEvent.OPEN,dbOpenedHandler);
sqlConnection.addEventListener(SQLErrorEvent.ERROR,dbErrorHandler);
sqlConnection.open(file);
getData();
}
public function loaded(event:Event):void {
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
for(var x:int=0;x<nupdates;x++){
var filename:Object=xmlcollection.getItemAt(x);
newfile=filename['pdfimage'];
writeAirFile();
}
}
public function writeAirFile():void {
/*
var obj:Object=new Object;
startdownload=true;
var file:File = File.applicationStorageDirectory.resolvePath(newfile);
fileStream = new FileStream();
fileStream.openAsync(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.addEventListener(Event.COMPLETE,progressHandler,true,10,true);
for(var x:int=0;x<fileData.length;x++){}
startdownload=false;
fileStream.close();
*/
}
public function progressHandler(event:Event){
Alert.show("File downloading");
}
public function dbOpenedHandler(event:SQLEvent):void{}
public function dbErrorHandler(error:SQLError):void{}
public function getData():void{
sql = "SELECT STRFTIME('%Y-%m-%d %H:%M:%S',lupdated_dt)as lupdated_dt FROM update_mas where delflag=0 ";
stmt = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.addEventListener(SQLEvent.RESULT, selectResult);
stmt.addEventListener(SQLErrorEvent.ERROR, selectError);
stmt.execute();
}
public function selectResult(event:SQLEvent){
DGArray.removeAll();
number=1;
var result = stmt.getResult();
var numRows = result.data.length;
for (var i = 0; i < numRows; i++){
for(var col:String in result.data[i]){
timestamp=result.data[i][col];
}
}
updateRequest.url="serverpath ?time="+timestamp+"";
updateRequest.send(null);
}
public function selectError(event):void{
stmt.removeEventListener(SQLEvent.RESULT, selectResult);
stmt.removeEventListener(SQLErrorEvent.ERROR, selectError);
Alert.show("SELECT error:");
}
public function displayPOPUP(event:ResultEvent):void{
nupdates=((int)(event.result));
if(nupdates==0){
}else{
selection=Alert.show(""+nupdates+"Updates available", "UPDATES", Alert.YES|Alert.NO, this, alertClickHandler);
}
}
private function alertClickHandler(event:CloseEvent):void {
if (event.detail==Alert.YES){
downloadRequest.url="serverpath ?time="+timestamp;
downloadRequest.send(null);
}else
Alert.show("download cancelled");
}
public function displayResult(event:ResultEvent):void{
var record:String="";
sql="";
for(index=0;index<xmlcollection.length;index++){
var category:Object=xmlcollection.getItemAt(index);
sql="select Id FROM CATEGORY where delflag=0 and cat_name='"+category['catname']+"'";
stmt = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.addEventListener(SQLEvent.RESULT,checkRecord);
stmt.addEventListener(SQLErrorEvent.ERROR,checkError);
stmt.execute();
}
}
public function checkRecord(event:Event):void{
var category:Object=xmlcollection.getItemAt(index);
var path:String="";
var result:SQLResult = stmt.getResult();
if(result.data==null){
var sql:String= "INSERT INTO CATEGORY (cat_name, created_user,created_dt,updated_user,updated_dt,image_jpg,image_pdf) ";
sql += "VALUES ('"+category['catname']+"',";
sql +="'admin','"+category['cdate']+"','admin','"+category['udate']+"'"+category['pictimage']+"','"+category['pdfimage']+"' )";
stmt = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.addEventListener(SQLEvent.RESULT,insertRecord);
stmt.addEventListener(SQLErrorEvent.ERROR,insertError);
stmt.execute();
}else{
sql="";
sql="update CATEGORY set created_dt='"+category['cdate']+"', updated_dt='"+ category['udate']+"',image_jpg='"+category['pictimage']+"', image_pdf='"+category['pdfimage']+"' where delflag=0 and cat_name='"+category['catname']+"'";
stmt = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.addEventListener(SQLEvent.RESULT,updateResult);
stmt.addEventListener(SQLErrorEvent.ERROR,updateError);
stmt.execute();
}
userRequest.send(null);
var ludt_dt:Object=xmlcollection.getItemAt((xmlcollection.length-1));
timestamp="";
timestamp=ludt_dt['udate'];
sql="";
sql="update update_mas set lupdated_dt='"+timestamp+"' where delflag=0 ";
stmt = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = sql;
stmt.addEventListener(SQLEvent.RESULT,updateResult);
stmt.addEventListener(SQLErrorEvent.ERROR,updateError);
stmt.execute();
var temp:int=0;
for(var x:int=0;x<=xmlcollection.length-1;){
var urlstring:String="http://sidssoldc:81/lessons/ravi/";
var url:Object=xmlcollection.getItemAt(x);
urlstring+=url['pdfimage'];
path=url['pdfimage'];
while((path.indexOf('/',0)!=-1)){
path=path.slice(path.indexOf('/',0)+1,path.length);
}
urlReq=new URLRequest(urlstring);
var filename:Object=xmlcollection.getItemAt(x);
var loader:URLLoader=new URLLoader();
loader.dataFormat="binary";
selectstream=2;
loader.load(urlReq);
loader.addEventListener(Event.COMPLETE,function(event:Event){
var loader:URLLoader=(URLLoader)(event.target);
loader.dataFormat="binary";
var ofstream2:FileStream= new FileStream();
var ofstream1:FileStream= new FileStream();
var ofstream3:FileStream= new FileStream();
if(selectstream==1){
var ofile1:File = File.applicationStorageDirectory.resolvePath('images/pdf/'+path);
ofstream1.openAsync(ofile1, FileMode.WRITE);
ofstream1.writeBytes(loader.data, 0, loader.bytesTotal);
selectstream++;
}else if(selectstream==2){
ofstream1.close();
var ofile2:File = File.applicationStorageDirectory.resolvePath('images/pdf/'+path);
ofstream2.openAsync(ofile2, FileMode.WRITE);
ofstream2.writeBytes(loader.data, 0, loader.bytesTotal);
selectstream++;
}else if(selectstream==3){
ofstream2.close();
var ofile3:File = File.applicationStorageDirectory.resolvePath('images/pdf/'+path);
ofstream3.openAsync(ofile3, FileMode.WRITE);
ofstream3.writeBytes(loader.data, 0, loader.bytesTotal);
}
if(selectstream==3){
ofstream3.close();
}
});
x++;
temp=loader.bytesTotal;
checkStream.close();
}
/*urlStream=new URLStream();
urlStream.addEventListener(Event.COMPLETE, function(event:Event){
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
for(var x:int=0;x<nupdates;x++){
var filename:Object=xmlcollection.getItemAt(x);
newfile=filename['pdfimage'];
var obj:Object=new Object;
startdownload=true;
var file:File = File.applicationStorageDirectory.resolvePath(newfile);
var fileStream:FileStream = new FileStream();
fileStream.openAsync(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.addEventListener(Event.COMPLETE,progressHandler,true,10,true);
for(var x:int=0;x<fileData.length;x++){
startdownload=false;
fileStream.close();
}
});
urlStream.load(urlReq);
x++;*/
}
public function insertRecord(event:Event):void{}
public function insertError(event:Event):void{}
public function updateResult(event:Event):void{}
public function updateError(event:Event):void{
Alert.show("update failed");
}
public function checkError(event:Event):void{
Alert.show("error");
}
public function showError(event:FaultEvent):void{
Alert.show(""+event.fault);
}
public function itemClick(event:ListEvent):void{}
]]>
</mx:Script>
<mx:DataGrid id="dgUserRequest" sortableColumns="false" x="99" y="115" width="364" textAlign="left" itemClick="itemClick(event);" rowHeight="30" doubleClickEnabled="false" height="297" themeColor="#ACF4F8" visible="true" editable="false" dataProvider="{catxmlcollection}" color="#000C0E" fontSize="11" >
<mx:columns>
<mx:DataGridColumn id="ID" headerText="Id" visible="false" dataField="catid" />
<mx:DataGridColumn id="snumber" width="70" headerText="SerialNo" dataField="sno" visible="true" />
<mx:DataGridColumn id="CATEGORY3" width="250" headerText="CATEGORY" dataField="catname" visible="true" />
</mx:columns>
</mx:DataGrid>
<mx:XMLListCollection id="catxmlcollection" source="{userRequest.lastResult.categories.category}"/>
<mx:HTTPService id="userRequest" url="http://192.168.10:81/lessons/ravi/cat.php" fault="showError(event);" useProxy="false" method="GET" resultFormat="e4x" />
<mx:HTTPService id="updateRequest" result="displayPOPUP(event);" fault="showError(event);" method="GET" > </mx:HTTPService>
<mx:XMLListCollection id="xmlcollection" source="{downloadRequest.lastResult.Categories.Category}" />
<mx:HTTPService id="downloadRequest" result="displayResult(event);" fault="showError(event);" useProxy="false" method="GET" resultFormat="e4x" ></mx:HTTPService>
<mx:HTTPService id="categoryRequest"></mx:HTTPService>
<mx:Label x="216" y="53" text="Category Master" fontWeight="bold" fontSize="12" width="151"/>
</mx:WindowedApplication>