ファイルのダウンロード

  fileDownload.mxml



自ドメインに置いて、自ドメイン内のファイルをダウンロードさせるツールです。

viewSourceURL は、このページへジャンプする為の記述で、右クリックのメニューに追加されます

ダウンロードするターゲットは、fileDownload.swf?dir=相対ディレクトリ&file=ファイル名&ext=拡張子 となります。
全て省略すると、swf が存在する位置の下にある file ディレクトリの readme.txt になります。
( テスト時に使用して下さい )

※ HGP創英角ポップ体 は、Microsoft Office のフォントです。

このファイルでは大きなファイルの処理を想定していませんが、その場合は Application の scriptTimeLimit
プロパティを設定する必要があるかもしれません。また、その場合はキャンセルボタンを実装して、FileReference の
cancel メソッドを呼ぶ必要があります。

↓fileDownload.swfのダウンロード

  
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
	xmlns:mx="http://www.adobe.com/2006/mxml"
	creationComplete="initApp()"
	backgroundColor="0xFFFFFF"
	horizontalAlign="left"
	paddingLeft="0"
	paddingTop="0"
	paddingBottom="0"
	paddingRight="0"
	viewSourceURL="http://winofsql.jp/VA003334/flex2071208145032.htm"
>
<mx:Style>

	@font-face {
		src: local("HGP創英角ポップ体");
		fontFamily: HGP;
		fontWeight: bold;
		unicodeRange: 
			U+0030-U+0039,
			U+30A6-U+30FC,
			U+0044-U+0044,
			U+0025-U+0025,
			U+003A-U+003A,
			U+0043-U+0043,
			U+002C-U+002C,
			U+002E-U+002E,
			U+5B8C-U+5B8C,
			U+4E86-U+4E86,
			U+0061-U+007A;
	}

	global {
		font-family: HGP;
		fontSize: 11px;
	}

</mx:Style>
<mx:Script>
<![CDATA[
	import mx.controls.Alert;
	import mx.utils.ObjectUtil;
	import flash.events.*;
	import flash.net.*;

	private var targetDir:String;
	private var targetFile:String;
	private var targetExt:String;
	private var fr:FileReference;
	private var size:String = "";

	private function initApp():void {

		var param:Object = mx.core.Application.application.parameters;

		targetDir = param['dir'];
		if ( targetDir == null || targetDir == "" ) {
			targetDir = "file";
		}
		targetFile = param['file'];
		if ( targetFile == null || targetFile == "" ) {
			targetFile = "readme";
		}
		targetExt = param['ext'];
		if ( targetExt == null ) {
			targetExt = "txt";
		}

		fr = new FileReference();

		// IO エラー
		fr.addEventListener(IOErrorEvent.IO_ERROR, systemError);
		// セキュリティエラー
		fr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, dummy);

		// 処理終了
		fr.addEventListener(Event.COMPLETE, completeUpload);
		// 処理中
		fr.addEventListener(ProgressEvent.PROGRESS, progressDownload);

		// キャンセル
		fr.addEventListener(Event.CANCEL, cancelDownload);

	}

	// *************************************************
	// 処理中
	// *************************************************
	private function progressDownload(event:ProgressEvent):void {

		// ダウンロード済みと最終データ量から進行状況をセット
		progressBar.setProgress(
			event.bytesLoaded,
			event.bytesTotal
		);

		// ラベルの表示
		if ( event.bytesTotal != 0 ) {
			size = nFormatter.format(event.bytesTotal);
			progressBar.label =
				 "Downloading " + 
				int((event.bytesLoaded/event.bytesTotal)*100) +
				"% : " + size;
		}

	}

	// *************************************************
	// 処理終了
	// *************************************************
	private function completeUpload(event:Event):void {

		progressBar.label = "ダウンロード完了 : " + size;

	}
	
	// *************************************************
	// 処理開始
	// *************************************************
	private function startDownload():void {

		var req:URLRequest = new URLRequest();

		req.url = targetDir+"/"+targetFile+"."+targetExt;

		req.method = URLRequestMethod.GET;

		// プログレスバーの初期化
		progressBar.setProgress(0, 100);

		size = "";

		// ダウンロード
		fr.download(req); 

	}
			
	// *************************************************
	// IO エラー
	// *************************************************
	private function systemError(event:IOErrorEvent):void{
		Alert.show("IOError:" + event.text);
	}

	// *************************************************
	// ファイル選択のキャンセル
	// *************************************************
	private function cancelDownload(event:Event):void {

	}

	// *************************************************
	// ダミー
	// *************************************************
	private function dummy(event:SecurityErrorEvent):void {
		Alert.show("Security Error:" + event.text);
	}
]]>
</mx:Script>

<!-- *********************************** -->
<!-- カンマ編集用非表示オブジェクト -->
<!-- *********************************** -->
<mx:NumberFormatter
	id="nFormatter"
	useThousandsSeparator="true"
/>

<mx:VBox>

	<!-- *********************************** -->
	<!-- ファイル選択 -->
	<!-- *********************************** -->
	<mx:Button
		width="100%"
		label="ダウンロード"
		click="startDownload();"
	/>

	
	<!-- *********************************** -->
	<!-- 実行中のプログレスバー -->
	<!-- *********************************** -->
	<mx:ProgressBar
		id="progressBar"
		mode="manual"
		label="Downloading 0%"
	/>

</mx:VBox>

</mx:Application>
  

IFRAME による設置
  
<IFRAME
	src="http://lightbox.on.coocan.jp/fileDownload.swf?dir=.&file=fileDownload&ext=swf"
	frameborder="no"
	scrolling="no"
	width="250"
	height="70"
></IFRAME>
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ