ファイルダウンロード用ツール

  重点項目



動的にスタイルを変更する為に、
StyleManager.getStyleDeclaration("適用対象").setStyle("属性",値); を使用

デフォルトのメニューを表示しないように、新たなコンテキストメニューを作成して、this.contextMenu にセットする
( Application に指定した viewSourceURL は表示されなくなるので注意 )

フォントより、使用する文字だけを指定し、swf に埋め込んで利用する

URL は、flashvars で引き渡す。

URLエンコードは swf の外部( JavaScript 等 ) で行う

↓ダウンロードページ
Flex : ファイルダウンロード用コンテンツ



  ソースコード



  
<?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"
	width="125"
>
<mx:Style>

	@font-face {
		src: local("HGP創英角ポップ体");
		fontFamily: HGP;
		fontWeight: nomal;
		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;
	}
	@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;
	}
	@font-face {
		src: local("HGP創英角ポップ体");
		fontFamily: HGP;
		fontWeight: italic;
		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 mx.styles.StyleManager;
	import flash.events.*;
	import flash.net.*;

	private var target:String;
	private var fr:FileReference;
	private var size:String = "";

	private function initApp():void {

		// 必要の無い右クリックメニューを非表示にする
		var cMenu:ContextMenu = new ContextMenu();
		cMenu.hideBuiltInItems();
		this.contextMenu = cMenu;

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

		target = param['target'];
		if ( param['type'] == 'red' ) {
			setStyle('backgroundGradientColors', [0x000000, 0x8A0029]);
			StyleManager.getStyleDeclaration("global")
				.setStyle("color",0xFFFFFF);
			StyleManager.getStyleDeclaration("Button")
				.setStyle("fillColors",[0xFFFFFF,0xFFFFFF]);
			StyleManager.getStyleDeclaration("Button")
				.setStyle("fillAlphas",[1,1]);
			StyleManager.getStyleDeclaration("Button")
				.setStyle("color",0x000000);
			StyleManager.getStyleDeclaration("Button")
				.setStyle("textRollOverColor",0x000000);
		}

		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 =				
				int((event.bytesLoaded/event.bytesTotal)*100) +
				"%";
		}

	}

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

		progressBarResult.text = "完了 : " + size;

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

		var req:URLRequest = new URLRequest();

		req.url = target;

		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="125"
		label="ダウンロード"
		click="startDownload();"
	/>

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

</mx:VBox>

</mx:Application>
  



  fileDownload.js

  
var createFileDownloadId="";
function createFileDownloadArea(id) {
	str="";
	str+="<div id=\""+id+"\"></div> ";
	createFileDownloadId = id;
	document.write(str);
}

function createFileDownload(url,swf,type,w,h,v) {
	str="<"+"script type=\"text/javascript\"> \n";
	str+="var flashvars = {}; \n";
	str+="flashvars.target = encodeURIComponent(\""+url+"\"); \n";
	str+="flashvars.type = \""+type+"\"; \n";
	str+="var params = {allowScriptAccess:\"always\",bgcolor:\"#ffffff\"}; \n";
	str+="var attributes = {}; \n";
	str+="swfobject.embedSWF( \n";
	str+="	\""+swf+"?reload=\"+(new Date()).getTime(), \n";
	str+="	\""+createFileDownloadId+"\",  \n";
	str+="	\""+w+"\", \n";
	str+="	\""+h+"\", \n";
	str+="	\""+v+"\", \n";
	str+="	\"\", \n";
	str+="	flashvars, \n";
	str+="	params, \n";
	str+="	attributes \n";
	str+="); \n";
	str+="<"+"\/script> \n";
	str+="<br>";
	str+="<a style='color:#0000FF;text-decoration:none;' href=\""+url+"\">";
	str+="ブラウザでダウンロード</a> ";
	document.write(str);
}
  



  二つ続けて貼り付けるサンプル

  
<script 
	charset="shift_jis"
	type="text/javascript"
	src="http://homepage2.nifty.com/lightbox/fileDownload.js"></script>

<script
	type="text/javascript"
	src="http://www.google.com/jsapi"
	charset="utf-8"></script>

<script type="text/javascript">
google.load("swfobject", "2.1"); 
createFileDownloadArea("flashcontent");
</script>

<script type="text/javascript">
createFileDownload(
"http://winofsql.jp/download/fileDownload_090513.lzh",
"http://homepage2.nifty.com/lightbox/fileDownload.swf",
"",125,90,"9.0.0");
</script>

<br><br>
↓レッドタイプ<br>

<script type="text/javascript">
createFileDownloadArea("flashcontent2");
</script>

<script type="text/javascript">
createFileDownload(
"http://winofsql.jp/download/fileDownload_090513.lzh",
"http://homepage2.nifty.com/lightbox/fileDownload.swf",
"red",125,90,"9.0.0");
</script>
  



  ヘッダに共有部分を貼り付ける場合

swfobject.js は、Google 経由で利用しなくても、
自分のサイトがある場合はそこに置いて使うほうが良いと思います

  
<script 
	charset="shift_jis"
	type="text/javascript"
	src="http://homepage2.nifty.com/lightbox/fileDownload.js"></script>

<script
	type="text/javascript"
	src="http://www.google.com/jsapi"
	charset="utf-8"></script>

<script type="text/javascript">
google.load("swfobject", "2.1"); 
</script>

  


createFileDownloadArea は、DIV を作成しているだけなので、HTML で書いてしまってもかまいません。

  
<script type="text/javascript">
createFileDownloadArea("flashcontent");
</script>

<script type="text/javascript">
createFileDownload(
"http://winofsql.jp/download/fileDownload_090513.lzh",
"http://homepage2.nifty.com/lightbox/fileDownload.swf",
"",125,90,"9.0.0");
</script>

<br><br>
↓レッドタイプ<br>

<script type="text/javascript">
createFileDownloadArea("flashcontent2");
</script>

<script type="text/javascript">
createFileDownload(
"http://winofsql.jp/download/fileDownload_090513.lzh",
"http://homepage2.nifty.com/lightbox/fileDownload.swf",
"red",125,90,"9.0.0");
</script>
  










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





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

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ