View Full Version : Importing flash.filesystem in laszlo
gargidas15
05-19-2009, 01:24 AM
Hi all,
Can anyone please tell me that how can I import flash.filesystem.*,JPGEncoder to access the methods of it? I'm using laszlo 4.3.0 in my application. I've developed a code to save an image directly to file through actionscript. That's why these packages are needed. But it is giving compilation error when i'm compiling it in swf8 as well as in swf9. Please do help me. It is very necessary for my application.
Here's my code...............
<canvas debug="true">
<view id="compositeView" name="sunset" width="260" height="220" bgcolor="blue">
<view x="40" y="20" width="200" height="200" source="SUNSET2.JPG" stretches="both"/>
<switch >
<when property="$as3" >
<passthrough>
import flash.fileSystem.*;
import flash.utils.ByteArray;
// import encoding.JPGEncoder;
import com.adobe.images.JPGEncoder;
import flash.display.Bitmap;
import flash.display.BitmapData;
</passthrough>
</when>
</switch>
<method name="getScreenshot" >
<![CDATA[
var mc = this.getMCRef();
var bmpd:BitmapData = new flash.display.BitmapData(this.width, this.height);
bmpd.draw(mc);
var jpgenc:JPGEncoder = new JPGEncoder(80);
var imgByteArray:ByteArray = jpgenc.encode(bmpd);
var fl:File = File.applicationStorageDirectory.resolvePath("images/snapshot.jpg");
var fs:FileStream = new FileStream();
fs.open(fl,FileMode.WRITE);
fs.writeBytes(imgByteArray);
fs.close();
]]>
</method>
</view>
<button x="100" y="300">Click to save image
<handler name="onclick">
compositeView.getScreenshot();
</handler>
</button>
</canvas>
Gargi
mjessup
05-19-2009, 07:33 AM
If your code is verbatim I notice you have the filesystem with a capital 'S' instead of lower case. With swf8 and swf9 the code I have used the swf8 (as2) impotrs are different than swf9+ (as3). What I have done is:
<switch>
<when property="$runtime" value="dhtml">
<!-- intentional compiler error for code that only runs in flash -->
<ERROR />
</when>
<when property="$runtime" value="swf8">
<!-- SWF8/AS2 code -->
</when>
<otherwise>
<!-- SWF9+/AS3 code -->
</otherwise>
</switch>
gargidas15
05-19-2009, 11:22 AM
Thanks for your reply. I've rectified the above code. But still it is giving the same errors. I think I've to import the library file for using filesystem,JPGEncoder packages. Because these are seems to be unknown in my code. But how I can I 'll be able to do this? Please help me.
Gargi
mjessup
05-20-2009, 05:25 AM
Hi Gargi,
Let me just qualify my answers by saying I have very little experience with flash. The only work I have done was to use their Socket class, so take my replies with a grain of salt.
I went to the adobe docs for flash 3:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/package-summary.html
And in the description for flash.filesystem it says:
The flash.filesystem package contains classes used in accessing the filesystem. This package is only available to content running in the AIR runtime.
I'm not exactly sure what the "AIR runtime" is but from a quick search it appears to be a separate runtime from standard flash. I looked in the LPS and noticed that in my installations of LPS 4.2.0 and 4.3.0 there was a directory "WEB-INF\frameworks\libs\air". Whether that is the same air referred to in Adobe's docs, or if it maybe a subset I'm not sure. I tried compiling your code in my installation and got the same error.
When I searched for the JPEG encoder I ended up at an external library site hosted on google code. I did some poking around on the forums and came across this thread (http://forum.openlaszlo.org/showthread.php?t=12990) discussing using some library that isn't a part of laszlo (there are also references to AIR). and how to install/use it, so that may give you some guidance about how to get the library working with laszlo.
gargidas15
05-20-2009, 10:46 AM
hi,
My problem with JPGEncoder is now solved. I've to use the as3 core library externally in the WEB-INF/classes folder. But I've still problem with importing flash.filesystem. Not getting actually what to do? Is there any package externally available to use filesystem in laszlo.
Gargi
mjessup
05-20-2009, 11:59 AM
Hi Gargi, If you are trying to use the regular flash embedded in a webpage you won't be able to write to the user's filesystem because of the flash security model. The only access that is possible with the current version is to upload a file from the flash app to your server. There is a component in the inubator that I have used for this:
LPSROOT/lps/components/incubator/uploader
If you want full access to the file system via flash.filesystem then you need to run your application in the AIR runtime, which after some further research is a seperate runtime for flash applications (like the java runtime). It runs as a desktop application instead of as a web application. THere are instructions in this blog (http://www.antunkarlovac.com/blog/2008/02/19/writing-an-openlaszlo-air-application/) about creating a special build of laszlo to write applications for AIR. Otherwise I don't think its possible unless in a future release adobe adds some kind of signed/trusted feature like java applets use for local file access.
gargidas15
05-21-2009, 06:37 PM
Hi,
ok. Thanks for your reply. you have helped me to overcome my confusion about filesystem. But for my application I need to save the image in a local disk of user. How can i'll be able to achieve that?
Gargi
mjessup
05-22-2009, 01:06 PM
Hi Gargi,
The answer really depends on your application requirements. If you are able to run it as a desktop application then you can try to develop using the AIR runtime with the references from my previous post (I have not personally done anything with it).
If your app must be a "traditional flash application" (i.e. the application running inside of a browser served from a website) then the following five step process is an approach you can try:
Encode your image data as something suitable for an HTTP transaction (i.e. Hex, BASE64, etc)
Create an HTTP post request and use the encoded data as the post body
Have the server save the encoded data to itself
Have the server send its reply as a unique identifier for the saved data
Open a browser window to a server location using the UID and use the browser to save the image data
For the request you should be able to use a datset and set the postbody attribute to your encoded data. You can then use doRequest to send the data to the server. Once that is invoked you should be able to listen for the events of the dataset for the server response.
Once you receive the response you can inspect the dataset and find the UID, then use the browser service to open a constructed URL directed to the file on the server. Here is some rough semi-pseudo-code for the idea (I'm not at my dev machine so this code will likely have errors):
<dataset name="saveDS" cacheable="false" clientcahceable="false"
src="http://myserver.com/uploadimage"
request="false" method="POST" />
<attribute name="requesting" type="boolean" value="false" />
<attribute name="dataDel" type="expression" />
<attribute name="errorDel" type="expression" />
<method name="saveImage"><![CDATA[
if(this.requesting){
Debug.error('already processing a save request');
return;
}
this.requesting = true;
//Set the post body for the request
saveDS.setAttribute('postbody', encode(imageData));
//Create delegates (if necessary) to read the server response
if(!this['dataDel']){
this['dataDel'] = new lz.Delegate(this, 'readResponse', saveDS, 'ondata');
}
if(!this['errorDel']){
this['errorDel'] = new lz.Delegate(this, 'saveFailed');
this['errorDel'].register(saveDS, 'onerror');
this['errorDel'].register(saveDS, 'ontimeout');
}
saveDS.doRequest();
]]></method>
<method name="encode" args="imgdata"><![CDATA[
var encodedData; //the character encoded data
//encode the image data appropriately
encodedData = ...
return encodedData;
]]></method>
<method name="readResponse" args="ignore=null"><![CDATA[
//Check any status codes, etc as sent by the server
//read the UID from some location in the dataset DOM
var uid = saveDS.getAttr('uid');
this.requesting = false;
//Open a link back to the server in the browser to
//retrieve and save the file
lz.BrowserService.loadURL(
'http://myserver.com/getImage?uid='+uid, '_new');
]]></method>
<method name="saveFailed" args="ignore=null"><![CDATA[
Debug.error('Data failed to save');
this.requesting = false;
]]></method>
You will then also need to write two pieces of code in your favorite server language. The first to read the encoded data, store it on the server, and send a reply in a valid XML structure so it can be loaded by the dataset, and a second that will do a passthrough of the image file when requested by the client from the 'getImage' URL with the appropriate HTTP headers.
This should provide you with a way around the flash security model, but it will cost you the transmission of the image data twice (once from client to server and once from server back to client) and the encoding/decoding time of the data (again twice, encoding on the client, then decoding on the server) as well as any temporary/ permanent storage space on the server for the image data for the post/ retrieve cycle. Depending on the size of these images it may be a negligible additional cost, but nonetheless something to consider.
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.