
Код AS3:
//LoadCompFileDocAndSendToServer
package {
import flash.display.*;
import flash.text.*;
import flash.net.*;
import flash.events.*;
import flash.utils.*;
import fl.controls.*;
public class LoadCompFileDocAndSendToServer extends Sprite {
private const php:String = 'http://www.murmadillo.tut.su/m/gallery/save-file.php';
private var loader:Loader,
file:FileReference,
txt:TextField,
sender:ByteArray,
i:int,
fileName:String,
request:URLRequest,
urlLoader: URLLoader,
strFileNoEx:String,
reg:RegExp,
general:Sprite,
progressBar:ProgressBar;
public function LoadCompFileDocAndSendToServer() {
//stage.scaleMode = StageScaleMode.NO_BORDER;
//stage.align = StageAlign.TOP_LEFT;
//stage.
addChild(general = new Sprite);
txt=new TextField;
txt.autoSize=TextFieldAutoSize.LEFT;
file=new FileReference;
// file.browse(getTypes());
file.addEventListener(Event.COMPLETE,onComplete);
file.addEventListener(Event.SELECT,onSelectFile);
//onShowloadDialog();
txt.htmlText="<a href=\'event:load\'><u>Загрузить jpg файл c компьютера и сохранить на сервере</a>;";
addChild(txt);//добавить на рабочее поле
txt.autoSize=TextFieldAutoSize.LEFT;
addChild(txt);
txt.addEventListener(TextEvent.LINK,txtLinkFunc);
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandlerUrlLoader);
addChild(progressBar = new ProgressBar);
progressBar.visible = false;
addChild(txt=new TextField);
}
private function txtLinkFunc(evt:TextEvent):void {
if (evt.text == 'load') {
onShowloadDialog();
}
}
//отображение диалога открытия файла
private function onShowloadDialog(event:Event = null):void {
txt.text = '';
var fileFilter:FileFilter=new FileFilter("Images","*.jpg");
try {
file.browse([fileFilter]);
}
//catch(illegalOperation:IllegalOperationError){
catch (e:Error) {
}
}
//при выборе файла загружаем его
private function onSelectFile(event:Event):void {
try {
file.load();
}
// catch(illegalOperation:IllegalOperationError)
catch (e:Error) {
}
}
private function onComplete(event:Event):void {
loader = new Loader;
loader.loadBytes(file.data);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderPicComplete);
//отправляем на сервер
reg = /\.\w+/;//регулярка вылавливающая расширение файла
strFileNoEx = file.name.replace(reg,'');
trace(strFileNoEx);//имя загруженного файла без расширения
request = new URLRequest(
php+"?name="+strFileNoEx+"&d="+new Date().getTime());
request.data = file.data;
request.method = URLRequestMethod.POST;
request.contentType = 'application/octet-stream';
urlLoader.load(request);
progressBar.visible = true;
addChild(progressBar);
}
private function completeHandlerUrlLoader(event:Event):void {
trace(urlLoader.data);
txt.autoSize=TextFieldAutoSize.LEFT;
txt.y = 200;
txt.htmlText = "<font color='#ff0000' size='30'> Файл на сервере";
progressBar.visible = false;
}
private function loaderPicComplete(evt:Event):void {
var sp:MovieClip;
var i:int,scale:Number,sh:Shape;
general.addChild(sp = new MovieClip);
sp.addChild(loader);
//если маленькая увеличим
if (sp.width < stage.stageWidth && sp.height < stage.stageHeight) {
for (i = 0; i < 500; i++) {
scale = i / 100;
if (sp.width * scale > stage.stageWidth && sp.height * scale > stage.stageHeight) break;
}
loader.content.scaleX = loader.content.scaleY = scale;
}
else
//если большая уменьшим
{
for (i = 500; i > 0; i--) {
scale = i / 100;
if (sp.width * scale < stage.stageWidth && sp.height * scale < stage.stageHeight) break;
}
loader.content.scaleX = loader.content.scaleY = scale;
}
//центровка
loader.x = (stage.stageWidth - sp.width ) / 2;
loader.y = (stage.stageHeight - sp.height ) / 2;
//белый фон
sp.addChildAt(sh = new Shape, 0);
sh.graphics.beginFill(0xFFFFFF);
sh.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
}
}
}
//php скрипт принимает имя файла без расширения и добавляет расширения jpg
/*
<?php
//save-file.php
$fileData = $GLOBALS[HTTP_RAW_POST_DATA];
$fileName = ''.$_GET['name'].'.jpg';
$fp = fopen($fileName, "w");
fwrite($fp,$fileData);
fclose($fp);
echo "file write - OK ".$fileName;
?>
*/