Loading...

2009年12月24日星期四

Flash Media Server 加上 JSON 功能

1) 到 http://www.json.org/ 找 ActionScript 1 的版本
2) 修改其中 JSON.error 的部份, 存成 JSON.asc
3) 將檔案放到 /scriptlib 或 application目錄
4) 在 main.asc 裡用「load( "JSON.asc" );」

修改後的 code:
/*
Copyright (c) 2005 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/*
Ported to Actionscript May 2005 by Trannie Carter ,
wwww.designvox.com

Ported to Actionscript 1 May 2006 by Malte Ubl ,
http://joose-js.blogspot.com

2006-11-18: Fixed assumption that an empty string evaluates to false in boolean
context that is wrong in Flash 8 an later.

2007-04-08: Fixed bad behavior with switch statements when publishing to Flash 6 from Flash MX.
Report and Patch by Dan Wich


Updated 2007-04-08

USAGE:
var o = JSON.parse(jsonStr);
var s = JSON.stringify(obj);

// If there is an error, the method calls _root.debug("Error message...")
// Override JSON.error for the desired behaviour

*/

JSON = new Object();
// shinder.lin modified 2009-12-24
JSON.error = function(msg) {
JSON.error_occured = true;
trace(msg);
}

JSON.stringify = function (arg) {
var c, i, l, s = '', v;
JSON.error_occured = false;

switch (typeof arg) {
case 'object':
if (arg) {
if (arg instanceof Array) {
for (i = 0; i < arg.length; ++i) {
v = JSON.stringify(arg[i]);
if (s != '') {
s += ',';
}
s += v;
}
return '[' + s + ']';
} else if (typeof arg.toString != 'undefined') {
for (i in arg) {
v = arg[i];
if (typeof v != 'undefined' && typeof v != 'function') {
v = JSON.stringify(v);
if (s != '') {
s += ',';
}
s += JSON.stringify(i) + ':' + v;
}
}
return '{' + s + '}';
}
}
return 'null';
case 'number':
return isFinite(arg) ? String(arg) : 'null';
case 'string':
l = arg.length;
s = '"';
for (i = 0; i < l; i += 1) {
c = arg.charAt(i);
if (c >= ' ') {
if (c == '\\' || c == '"') {
s += '\\';
}
s += c;
} else {
switch (c) {
case '\b':
s += '\\b';
break;
case '\f':
s += '\\f';
break;
case '\n':
s += '\\n';
break;
case '\r':
s += '\\r';
break;
case '\t':
s += '\\t';
break;
default:
c = c.charCodeAt();
s += '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}
}
}
return s + '"';
case 'boolean':
return String(arg);
default:
return 'null';
}
}



JSON.parse = function (text) {
var at = 0;
var ch = ' ';
JSON.error_occured = false;


function error(m) {
JSON.error("JSONError: "+m)
}

function next() {
ch = text.charAt(at);
at += 1;
return ch;
}

function white() {
while (!JSON.error_occured && ch != null) {
if (ch <= ' ') {
next();
} else if (ch == '/') {
next();
switch (ch) {
case '/':
while (!JSON.error_occured && next() != null && ch != '\n' && ch != '\r') {}
break;
case '*':
next();
while (true) {
if (ch) {
if (ch == '*') {
if (next() == '/') {
next();
break;
}
} else {
next();
}
} else {
error("Unterminated comment");
}
}
break;
default:
error("Syntax error");
}
} else {
break;
}
}
}

function str() {
var i, s = '', t, u;
var outer = false;

if (ch == '"') {
while (!JSON.error_occured && next() != null) {
if (ch == '"') {
next();
return s;
} else if (ch == '\\') {
next();
switch (ch) {
case 'b':
s += '\b';
break;
case 'f':
s += '\f';
break;
case 'n':
s += '\n';
break;
case 'r':
s += '\r';
break;
case 't':
s += '\t';
break;
case 'u':
u = 0;
for (i = 0; i < 4; i += 1) {
t = parseInt(next(), 16);
if (!isFinite(t)) {
outer = true;
break;
}
u = u * 16 + t;
}
if(outer) {
outer = false;
break;
}
s += String.fromCharCode(u);
break;
default:
s += ch;
}
} else {
s += ch;
}
}
}
error("Bad string");
}

function arr() {
var a = [];

if (ch == '[') {
next();
white();
if (ch == ']') {
next();
return a;
}
while (!JSON.error_occured && ch != null) {
a.push(value());
white();
if (ch == ']') {
next();
return a;
} else if (ch != ',') {
break;
}
next();
white();
}
}
error("Bad array");
}

function obj() {
var k, o = {};

if (ch == '{') {
next();
white();
if (ch == '}') {
next();
return o;
}
while (!JSON.error_occured && ch != null) {
k = str();
white();
if (ch != ':') {
break;
}
next();
o[k] = value();
white();
if (ch == '}') {
next();
return o;
} else if (ch != ',') {
break;
}
next();
white();
}
}
error("Bad object");
}

function num() {
var n = '', v;

if (ch == '-') {
n = '-';
next();
}
while (!JSON.error_occured && ch >= '0' && ch <= '9') {
n += ch;
next();
}
if (ch == '.') {
n += '.';
next();
while (!JSON.error_occured && ch >= '0' && ch <= '9') {
n += ch;
next();
}
}
if (ch == 'e' | ch == 'E') {
n += ch;
next();
if (ch == '-' || ch == '+') {
n += ch;
next();
}
while (!JSON.error_occured && ch >= '0' && ch <= '9') {
n += ch;
next();
}
}
v = Number(n);
if (!isFinite(v)) {
error("Bad number");
}
return v;
}

function word() {
switch (ch) {
case 't':
if (next() == 'r' && next() == 'u' &&
next() == 'e') {
next();
return true;
}
break;
case 'f':
if (next() == 'a' && next() == 'l' &&
next() == 's' && next() == 'e') {
next();
return false;
}
break;
case 'n':
if (next() == 'u' && next() == 'l' &&
next() == 'l') {
next();
return null;
}
break;
}
error("Syntax error");
}

function value() {
white();
switch (ch) {
case '{':
return obj();
case '[':
return arr();
case '"':
return str();
case '-':
return num();
default:
return ch >= '0' && ch <= '9' ? num() : word();
}
}

return value();
}

2009年12月23日星期三

Flash取得Cookie

當然還是得透過 JavaScript 才會取得, 不過可以偷懶不用寫 JavaScript:

var s:String = ExternalInterface.call("document.cookie.toString");

通常會處理一下

var cookieStr:String = ExternalInterface.call("document.cookie.toString");
var cookieObj:Object = {};
cookieStr = cookieStr.split(' ').join('');
var tmp:Array = cookieStr.split(';');
for(var i in tmp) {
var tmp2:Array = tmp[i].split('=');
cookieObj[tmp2[0]] = unescape(tmp2[1]);
}
s_txt.text = cookieObj['PHPSESSID'];

2009年11月30日星期一

中文字的 unicode 範圍

主要是 U+4E00 至 U+9FA5
資料來源 請問一下 unicode 的編碼範圍問題

2009年8月10日星期一

Apache VirtuslHost 偵聽不同埠

環境 Apache 2.2

先在 httpd.conf 設定:

→ Listen 8080

→ Include conf/extra/httpd-vhosts.conf



接著在 conf/extra/httpd-vhosts.conf 設定:

→ NameVirtualHost localhost:8080

→ <VirtualHost localhost:8080>

→ DocumentRoot "C:/wamp/www/web8080"

→ ServerName localhost

→ ErrorLog "logs/web8080-error.log"

→ CustomLog "logs/web8080-access.log" common

→ </VirtualHost>



若要在本機上能 access 到某個 host,需要在C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS 加入一筆記錄

2009年8月3日星期一

PHP 的上傳設定

PHP 的上傳設定除了修改 upload_max_filesize 外
memory_limit, max_execution_time, post_max_size 也要適當修改
通常 memory_limit > post_max_size > upload_max_filesize

另外, Apache 的上傳控制是 LimitRequestBody

2008年8月28日星期四

在 UIComponent 中加入 Bitmap

Flex UIComponent 的 addChild() 只能加入 UIComponent 物件,
若要從 BitmapData 弄個 Bitmap 便無法加入。

Flex3 的 SWFLoader 可以解決這個問題,例如:
var bmp:BitmapData = new BitmapData(sw, sh);
bmp.draw(global.application.t_canvas, matrix);
var swf:SWFLoader = new SWFLoader();
swf.source = new Bitmap(bmp);
this.addChildAt(swf, 0);

2008年8月4日星期一

Flex in a Week

Flex in a Week
Adobe 官方影音教學, 應該要有 5 天的教學課程, 目前只有 3 天...
真的一個星期就可以學會 Flex 嗎? 這要看「會」的定義是什麼...

2008年6月24日星期二

動態改變 Flash Movie 的大小

原始網頁: BrowserCanvas. The World’s Easiest Way to Dynamically Resize Flash

2008年6月22日星期日

JavaScript Components

blueshoes.org 提供不少 JavaScript Components

另外, Swazz Calendar 是個滿讚的日期輸入月曆

2008年6月21日星期六

FileReference 上傳後得知檔案大小

上傳圖檔時, 同時上傳資料, 可以參考之前舊文: FileReference 上傳圖檔

利用偵聽 uploadCompleteData (DataEvent.UPLOAD_COMPLETE_DATA) 事件即可解決

Frame Actions
var uploadURL:String = "upload_test.php";
var req:URLRequest = new URLRequest(uploadURL);
var file:FileReference = new FileReference();

// 設定偵聽器
file.addEventListener(Event.SELECT, myFileSelect);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA , myUploadCompleteData);

// 瀏覽上傳的檔案
file.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);

// 選擇所欲上傳的圖檔後
function myFileSelect(e:Event):void{
file.upload(req);
}
// 上傳完成後
function myUploadCompleteData(e:DataEvent):void{
var data:String = e.data as String;
trace(data);
var filename = data.split('::')[0];
// 載入圖片
var picReq:URLRequest = new URLRequest(filename);
var pic:Loader = new Loader();
pic.load(picReq);
pic.y = 20;
this.addChild(pic);
// 秀出回傳的字串
var tf:TextField = new TextField();
tf.autoSize = 'left';
tf.text = data;
this.addChild(tf);
}

PHP
<?php
// 圖檔上傳後所欲存放的目錄
$up_dir = "./imgs/";
// 若目錄不存在, 則建立之
if(!is_dir($up_dir))
mkdir($up_dir, 0755);
// 取得上傳檔案的副檔名
$pos = strrpos($_FILES["Filedata"]["name"], ".");
if ($pos === false) {
$ext = "";
}else{
$ext = substr($_FILES["Filedata"]["name"], $pos);
}
// 檔案大小 bytes
$size = $_FILES['Filedata']['size'];
// 以隨機的字串為檔名
$uniq = md5(uniqid(rand(), true));
$up_file = $up_dir . $uniq . $ext;
// 將檔案放到設定的目錄內
move_uploaded_file($_FILES["Filedata"]["tmp_name"], $up_file);
chmod($up_file, 0777);

echo $up_file . '::' . $size;
?>