Good Morning Justice League of Stackoverflow,
I have here a problem that may stump the panel.
I am creating an interactive post-it for an upcoming event that allows for us to tap into a sql database and post tweets, survey answers and images. We've already tapped into the Twitter API and the survey, so those are A-OK.
The problem lies within loading the images from a location other than the local interactive board's server.
If the image itself is locally hosted, it loads just fine.
If the image is hosted elsewhere, the image will not load, even though I have a trace on the URL of said image.
I'm loading all tweets, surveys and images through an XML load and all the data is loading properly.
I AM loading the image through a smoothing filter so that when the "post-its" are slightly rotated, they are not jagged. Here is THAT code:
import flash.display.*;
var srcImg = _parent._parent.varContent;
urlText.text = srcImg;
var mainHolder = this.createEmptyMovieClip("main", this.getNextHighestDepth());
var original = mainHolder.createEmptyMovieClip("original", mainHolder.getNextHighestDepth());
var smooth = mainHolder.createEmptyMovieClip("smooth", mainHolder.getNextHighestDepth());
var mclListener:Object = new Object();
mclListener.onLoadInit = function() {
var w = original._width;
var h = original._height;
var bmpData1:BitmapData = new BitmapData(w, h, true, 0x000000);//true and 0 color allows for transparency
bmpData1.draw(original);
smooth.attachBitmap(bmpData1,2,"auto",true);//true for SMOOTHING, ;)
reSize(smooth);
original.removeMovieClip();
mainHolder._x = -(smooth._width / 2);
mainHolder._y = -(smooth._height / 2);
};
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
image_mcl.loadClip(srcImg,original);
function reSize(target) {
if (target._width > target._height) {
s = Math.floor((300.85 / target._height) * 100);
}
if (target._width < target._height) {
s = Math.floor((320.90 / target._width) * 100);
}
target._xscale = s;
target._yscale = s;
}
This is a two part script where the bulk loads in the image and places it into an empty movieclip, then adds the smoothing filter. The second part is a resizer that automatically resizes the image and keeps the aspect ratio
Here's the kicker. When I test the flash piece (not embedded in HTML) the thing works 100%.
As soon as I put the swf into an html and view it on a web page, the remote images will not load.
I'm a bit stumped on why this is, could this be a firewall or security issue? Because I work in a high security firewall environment.
Any guidance in this would be most appreciated.
Thank you for your time.
by default flash does not allow cross domain loading of data as a security feature, but it can be overridden.
this may help:
allowDomain (security.allowDomain method) if you can get a swf running on the image server
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002104.html
A cross domain policy file may also be used on the server to grant access to the swf:
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000470.html
Related
I've got a Flash app that calls an online php file in order to read some values of my SQL table.
So I've got a line like this in my AS3 code:
var urlReq:URLRequest = new URLRequest ("http://www.****.com/sql_result.php");
And this in my php :
$connection = mysql_connect("mysql***.perso", "test", "password") or die ("Couldn't connect to the server.");
Problem : if the user is offline he can't access the values.
Is there way to download the SQL table with AS3 code (when the user have internet) in order to access it offline.
Like :
function onConnection(e:Event = null):void{
if(monitor.available)
{
trace("You are connected to the internet");
read_php_online();
}
else
{
trace("You are not connected to the internet");
read_php_offline();
}
monitor.stop();
}
function read_php_offline():void{
var urlReq:URLRequest = new URLRequest ("local/sql_result_offline.php");
..
..
}
And what should have sql_result_offline.php in order to access an offline SQL Table ?
$connection = mysql_connect("LOCAL", "user", "password");
Thank you,
For FLASH :
To save data locally with flash, you can use one of 3 manners : the Flash Player cache, a SharedObject, or a FileReference object. And for your local file, forget PHP and MySQL because we are speaking only about the data that you got ( json, xml, txt, ... ).
- Flash Player cache :
You should know that by default, flash player put a local copy of your file in its cache. You can use this local copy as an offline source of your data, but here don't forget that flash player didn't save the last version of your remote file but the first one and that http://www.example.com/data.php is different from http://www.example.com/data.php?123 even if it's the same file ! For more details about that, take a look on my answer of this question.
- SharedObject :
I don't know the size of your loaded data, but as Adobe said about SharedObject :
... is used to read and store limited amounts of data on a user's computer ...
I think that is not used for large files and it's not recommended to store files but some simple data. Of course, as a cookie for the browser, SharedOject needs user's authorization to write data to the hard drive, and user can delete it at any time.
- FileReference :
I think this is the best manner to do what you are looking for. You should know that to save a file using FileReference, your user is invited to select a file for saving data and reading it in a second time. So if you don't want any user's interaction with your application, forget this manner.
FileReference using example :
var local_file_name:String = 'local.data',
file:FileReference = new FileReference(),
local_file_filter:FileFilter = new FileFilter('local data file', '*.data'),
remote_data_url:String = 'http://www.example.com/data.php',
url_request:URLRequest,
url_loader:URLLoader,
connected:Boolean = true;
if(connected){
get_remote_data();
} else {
get_local_data();
}
function get_remote_data(): void {
//we use a param to be sure that we have always the last version of our file
url_request = new URLRequest(remote_data_url + ('?' + new Date().getTime()));
url_loader = new URLLoader();
url_loader.addEventListener(Event.COMPLETE, on_data_loaded);
url_loader.load(url_request);
}
function get_local_data(): void {
// show the select dialog to the user to select the local data file
file.browse([local_file_filter]);
file.addEventListener(Event.SELECT, on_file_selected);
}
function on_data_loaded(e:Event): void {
var data:String = e.target.data;
// if the remote data is successfully loaded, save it on a local file
if(connected){
// show the save dialog and save data to a local file
file.save(data, local_file_name);
}
// use your loaded data
trace(data);
}
function on_file_selected(e:Event): void {
file.addEventListener(Event.COMPLETE, on_data_loaded);
file.load();
}
This code will show every time a save dialog to the user, of course, it's just a sample, you have to adapt it to your needs ...
EDIT
For AIR :
With AIR we don't need a FileReference object, instead we use File and a FileStream object to save data :
// for example, our local file will be saved in the same dir of our AIR app
var file:File = new File( File.applicationDirectory.resolvePath('local.data').nativePath ),
remote_data_url:String = 'http://www.example.com/data.php',
data_url:String = remote_data_url,
url_request:URLRequest,
url_loader:URLLoader,
connected:Boolean = true;
if(!connected){
// if we are not connected, we use the path of the local file
data_url = file.nativePath;
}
load_data();
function load_data(): void {
url_request = new URLRequest(data_url);
url_loader = new URLLoader();
url_loader.addEventListener(Event.COMPLETE, on_data_loaded);
url_loader.load(url_request);
}
function on_data_loaded(e:Event): void {
var data:String = e.target.data;
if(connected){
// save data to the local file
var file_stream:FileStream = new FileStream();
file_stream.open(file, FileMode.WRITE);
file_stream.writeUTFBytes(data);
file_stream.close();
}
trace(data);
}
Hope that can help.
you have a flash swf, mobile app or air app?
Storing local data
you can use file as database (like csv), for mobile and air you can use local SQLite database.
if you have native desktop app - it is possible to use mysql, via native process or native extension but it is not so easy..
edit:
Working with local SQL databases in AIR [+] you can keep your data safe- with encryption, a password at startup and etc. [-] it will require a lot more of code (create database after install, sync regularly, get data from local database if no internet conn.) mysql and sqlite have some differences also (like "insert or update" statement for sqlite)
I have a JQuery-Code like this:
var number_images = 5;
var ct = 1;
// Goto previous Picture
$('#changeImageMinus').on('click', function()
{
if(ct-1>=1 && ct-1<=number_images)
{
ct = ct - 1;
$('.container-bg').css("background-image","url('images/" + ct + ".png')");
}
})
// Goto next Picture
$('#changeImagePlus').on('click', function()
{
if(ct+1>=1 && ct+1<=number_images)
{
ct = ct + 1;
$('.container-bg').css("background-image","url('images/" + ct + ".png')");
}
})
I have images named like 1.png, 2.png ... in the folder images/, so i simply load the images back/forward by pressing a "+" or a "-" button. The problem ist that the loading takes really long and i would like to know if there is a possible way to preload all images into a buffer or something like that. I basically want to load the images all before the site openes so that it will show off faster when i switch them. Thank you very much in advance!
You can do it with JS by using Image class. Create new Image object and set it's src property with path to your picture. Do it for all images before printing page and you should have them preloaded. In this article they describe it (second way).
Just be careful because if you have a lot of pictures it can negatively affect the user experience, especially for the users with slower connection (yes they exist, me for example :D). Imagine that you need to wait few (like 10 or more) seconds for a page to load. The best method in such cases would be preloading the specified amount of images and then loading the rest if needed. The problem of waiting may occure again then but at least user will see your page and not search for some other one :)
I have been trying to create smoothly functioning image preview for each website URL, on hover, using the websnapr service. The javascript below works great, but generates a website preview on the fly, which is very slow, and seems unnecessary because I am always working with the same database entries, and this method may generate the image preview for the SAME website multiple times (on every refresh), when in actuality only once will suffice, as long as I have and can store the image path in a DB for later use.
The database I am working with contains 3,000+ website URL's. I would like to create a thumbnail image preview for each of these in an automated way, using a PHP script and by storing the websnapr image paths in the database, so they can later be used to generate website previews in a more efficient way. However, this is a bit over my skill level...
I appreciate any suggestions how to do this.
Many thanks in advance!
JavaScript
$('.co-website').each(function(){
var url = encodeURIComponent($(this).children('a').attr('href'));
var imgSRC = 'http://images.websnapr.com/?url=' + url + '&key=' + apiKey + '&hash=' + encodeURIComponent(websnapr_hash);
$(this).children('a').attr("title", "<img src='"+imgSRC+"' />");
});
PHP Script
while($row = mysqli_fetch_array($results)){
$thumbnail_url = 'http://images.websnapr.com/?url=' . $row['website'] . '&key='.$apiKey.'&hash='.;
//Not sure how to get the hash portion of the URL
}
//update this record's thumbnail mySQL field
I am working on a project, in which i need to announce an announcement when a txt file is created on the server and i need to notify all users through an audio announcement,the audio should be played at once on any client browsers that are currently on the pages. the playing of the announcement needs to be synchronized upto maximum accuracy.
the announcement is composed of multiple audio files (playlist).
after the announcement is played on all active clients the txt file will be deleted. and the server will be waiting/looking for another txt file.
for example:
client1 - server time: 19:22:01, Recieved announcement and playing the audio
Client2 - server time: 19:22:01, Recieved announcement and playing the audio
any recommendations? on how to accomplish the announcement at once on all clients, any technique? mysql database or
Flash, Applets, HTML5 audio, JQuery etc.
Thanks..
I wrote a long-poller technique with simple PHP, Ajax en MySQL:
The PHP code is as follows:
timeout = 600;
while (timeout > 0) {
$res = db_query("QUERY");
$return_value = create_value_from_result($res);
// see if the result changed
$db_hash = md5($return_value);
if ($_SESSION['hash'] == $db_hash) {
// the result didn't change -- sleep and poll again
// usleep take microseconds, 100000 is 100 millisecond
// this is the default database polling interval
usleep(100000);
$timeout--;
} else {
// the result changed -- reply the result and set the session hash
$timeout = 0;
$_SESSION['hash'] = $db_hash;
}
}
return json_encode($return_value);
And the Javascript is simple Ajax (dojo is this case):
function longpoll() {
dojo.xhrPost({
url: 'longpolling.php',
load: function (data, ioArgs) {
data = dojo.fromJson(data);
do_magic(data);
// use settimeout to avoid stack overflows
// we could also use a while(1) loop,
// but it might give browser errors such as 'script is
// running too long' (not confirmed)
setTimeout(longpoll, 0);
}
});
}
You need the 60 second timeout to make sure the browser doesn't timeout on the Ajax call.
This way, as soon as the result of QUERY changes (a record gets inserted, an update made on a record), the PHP call returns and the Ajax gets its result.
I need to accomplish the following and need help with #2 below
My site has a page with form and the submitted form data needs to be written to a database on my site.
After it is written to the database, the same data submitted on the form needs to be sent to a page that processes it on another site so as if the form submission came from a page on that other site. The page that processes it on the other site is a php page.
It's a bit unclear, but my guess is that you're trying to do a 'form post' to the other .php page after your data is written to the database.
You can more information from this wonderful Scott Hanselman article, but here is the summary:
public static string HttpPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy = new System.Net.WebProxy(ProxyString, true);
//Add these, as we're doing a POST
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream ();
os.Write (bytes, 0, bytes.Length); //Push it out there
os.Close ();
System.Net.WebResponse resp = req.GetResponse();
if (resp== null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
The ideal solution to your problem is that you create a web service on the php site and your asp.net code calls the web service. http://en.wikipedia.org/wiki/Web_service
Creating a web service in PHP: http://www.xml.com/pub/a/ws/2004/03/24/phpws.html
Calling a web service in ASP.Net: http://www.codeproject.com/KB/webservices/WebServiceConsumer.aspx
Alternatively you could create a http request from your asp.net to the php site posting all the form elements to the php site.
Here is an example: http://www.netomatix.com/httppostdata.aspx
NB: You are almost guaranteed to run into problems with the second approach in the medium to long term, I don't recommend it unless you don't have control over the php site.