ASP ファイルアクセス

  目次







  global.asa によるタイプライブラリ宣言



  
それぞれ、
{EF53050B-882E-4776-B643-EDA472E8E3F2} は、Microsoft ActiveX Data Objects 2.7 Library
{420B2830-E718-11CF-893D-00A0C9054228} は、Microsoft Scripting Runtime

を示します

この宣言によって、各COM コンポーネントでサポートされる定数を参照できるようになります
  

  
<!--METADATA TYPE="TypeLib" UUID="{EF53050B-882E-4776-B643-EDA472E8E3F2}" -->
<!--METADATA TYPE="TypeLib" UUID="{420B2830-E718-11CF-893D-00A0C9054228}" -->

<SCRIPT language=VBScript runat=Server> 

' **********************************************************
' アプリケーション共通
' **********************************************************
Sub Application_OnStart


End Sub
' **********************************************************
' セッション開始
' **********************************************************
Sub Session_OnStart


End Sub

' **********************************************************
' セッション終了
' **********************************************************
Sub Session_OnEnd


End Sub

</SCRIPT>
  

以下のコードで定数が有効かどうかテストする事ができます

  
<% Call Response.AddHeader( "Content-Type", "text/html; Charset=shift_jis" ) %>
<PRE>
ADO の定数
adTypeBinary = <%= adTypeBinary %>
adTypeText = <%= adTypeText %>

ファイルシステムオブジェクトの定数
ForReading = <%= ForReading %>
ForWriting = <%= ForWriting %>
ForAppending = <%= ForAppending %>
</PRE>
  

  
ADO の定数
adTypeBinary = 1
adTypeText = 2

ファイルシステムオブジェクトの定数
ForReading = 1
ForWriting = 2
ForAppending = 8
  



  テキストファイルの処理

読み込み
  
<% Call Response.AddHeader( "Content-Type", "text/html; Charset=shift_jis" ) %>
<PRE>
<%
' **********************************************************
' オブジェクト作成
' **********************************************************
	Set Fs = Server.CreateObject("Scripting.FileSystemObject")

' **********************************************************
' ファイルオープン
' **********************************************************
	on error resume next
	Set obj = Fs.OpenTextFile( Server.MapPath("/FileAccess/global.asa"), ForReading )
	if Err.Number <> 0 then
		Response.Write "ファイルを開けませんでした"
		Response.Write "</PRE>"
		Response.End
	end if
	on error goto 0

' **********************************************************
' 処理
' **********************************************************
	Do While not obj.AtEndOfStream
		Buffer = obj.ReadLine
		Response.Write Server.HTMLEncode( Buffer ) & vbCrLf
	Loop

' **********************************************************
' ファイルクローズ
' **********************************************************
	obj.Close
%>
</PRE>
  


書き込み
  
<% Call Response.AddHeader( "Content-Type", "text/html; Charset=shift_jis" ) %>
<PRE>
<%
' **********************************************************
' オブジェクト作成
' **********************************************************
	Set Fs = Server.CreateObject("Scripting.FileSystemObject")

' **********************************************************
' ファイルオープン
' **********************************************************
	on error resume next
	Set obj = Fs.OpenTextFile( Server.MapPath("ServerVariables.txt"), ForWriting, True )
	if Err.Number <> 0 then
		Response.Write "ファイルを開けませんでした"
		Response.Write "</PRE>"
		Response.End
	end if
	on error goto 0

' **********************************************************
' 処理
' **********************************************************
	For Each strKey In Request.ServerVariables
		obj.WriteLine strKey & " = " & Request.ServerVariables( strKey )
	Next

' **********************************************************
' ファイルクローズ
' **********************************************************
	obj.Close

' **********************************************************
' テキストファイルを一度に全て読み込む
' **********************************************************
	Set obj = Fs.OpenTextFile( Server.MapPath("ServerVariables.txt"), ForReading )
	Response.Write obj.ReadAll
	obj.Close
%>
</PRE>
  

ForAppending を書き込み時に使用すると、追加書き込みになります



  バイナリファイルの処理

読み込み ( 画像コンテンツ )
  
<%
Response.ContentType = "image/JPEG"
 
' **********************************************************
' オブジェクト作成
' **********************************************************
	Set Stream = Server.CreateObject("ADODB.Stream")
 
' **********************************************************
' ストリームオープン
' **********************************************************
	Stream.Open
 
' **********************************************************
' Stream タイプの指定
' **********************************************************
	Stream.Type = adTypeBinary
 
' **********************************************************
' 既存ファイルの内容を Stream に読み込む
' **********************************************************
	Stream.LoadFromFile Server.MapPath( "sample.jpg" )
 
' **********************************************************
' バイナリ型の Stream オブジェクトからを読み取って出力
' **********************************************************
	Response.BinaryWrite Stream.Read
 
' **********************************************************
' Stream を閉じる
' **********************************************************
	Stream.Close
 
%>
  

書き込み ( ファイルのアップロード )
  
<% Call Response.AddHeader( "Content-Type", "text/html; Charset=shift_jis" ) %>
<FORM
	method=POST
	enctype=multipart/form-data
>
<INPUT type=file name=upload>
<INPUT type=submit name=send value="送信">
</FORM>
<%
if Request.ServerVariables( "REQUEST_METHOD" ) = "POST" then
 
' **********************************************************
' オブジェクト作成
' **********************************************************
	Set Stream = Server.CreateObject("ADODB.Stream")
 
' **********************************************************
' ストリームオープン
' **********************************************************
	Stream.Open
 
' **********************************************************
' Stream タイプの指定
' **********************************************************
	Stream.Type = adTypeBinary
 
' **********************************************************
' raw データの受信と書き込み
' **********************************************************
	nCount = Request.TotalBytes
	Stream.Write Request.BinaryRead( nCount )
	Stream.SaveToFile Server.MapPath("Post.dat"), _
		adSaveCreateOverWrite 

' **********************************************************
' Stream を閉じる
' **********************************************************
	Stream.Close
 
end if
%>
  

以下は post.dat の内容 (テキストファイルの場合)

  
-----------------------------7d52352d20294
Content-Disposition: form-data; name="upload"; filename="C:\TEMP\upload.txt"
Content-Type: text/plain

テキストファイルのアップロード時は、VBScript でファイル
のオリジナル部分を比較的簡単に取り出す事ができますが、
バイナリファイルの場合は少し複雑になります

-----------------------------7d52352d20294
Content-Disposition: form-data; name="send"

送信
-----------------------------7d52352d20294--
  

以下はバイナリファイルの切り出し処理です

  
<% Call Response.AddHeader( "Content-Type", "text/html; Charset=shift_jis" ) %>
<FORM
	method=POST
	enctype=multipart/form-data
>
<INPUT type=file name=upload>
<INPUT type=submit name=send value="送信">
</FORM>
<%
if Request.ServerVariables( "REQUEST_METHOD" ) = "POST" then
 
' **********************************************************
' オブジェクト作成
' **********************************************************
	Set Stream = Server.CreateObject("ADODB.Stream")
 
' **********************************************************
' ストリームオープン
' **********************************************************
	Stream.Open
 
' **********************************************************
' Stream タイプの指定
' **********************************************************
	Stream.Type = adTypeBinary
 
' **********************************************************
' raw データの受信と書き込み
' **********************************************************
	nCount = Request.TotalBytes		' 受信総バイト数
	RawData = Request.BinaryRead( nCount )	' バイト配列取得
	pos = InStrB( RawData, ChrB(AscB(vbCr)) )
	Target = LeftB( RawData, pos )
	pos = InStrB( pos+1, RawData, ChrB(AscB(vbCr)) )
	pos = InStrB( pos+1, RawData, ChrB(AscB(vbCr)) )
	pos = InStrB( pos+1, RawData, ChrB(AscB(vbCr)) )
	pos2 = InStrB( pos+1, RawData, Target )

	' 一旦全てをストリームへセット
	Stream.Write RawData
	' バイナリデータの開始位置( ストリームは 0 開始 )
	Stream.Position = pos + 1
	' ターゲットデータを取得
	BinDataArray = Stream.Read( pos2 - ( pos+2 ) - 2 )
	' ストリーム初期化
	Stream.Position = 0
	Stream.SetEOS
	' ターゲットデータをストリームへセット
	Stream.Write BinDataArray
	' ファイルへ書き込み
	Stream.SaveToFile Server.MapPath("Post.dat"), _
		adSaveCreateOverWrite 

' **********************************************************
' Stream を閉じる
' **********************************************************
	Stream.Close

end if
%>
  



  Web ファイルダウンロード

  
<%
Call Response.AddHeader( "Content-Type", "text/html; Charset=shift_jis" )
Response.ExpiresAbsolute=#May 31,2000 23:59:59#

Dim strMessage

' **********************************************************
' MODEL
' **********************************************************
function Download()

	if Trim( Request.Form("url") ) = "" then
		strMessage = "URL を入力して下さい"
		Exit Function
	end if

	Set Http = Server.CreateObject("Msxml2.ServerXMLHTTP")
	on error resume next
	Call Http.Open("GET", Request.Form("url"), False )
	if Err.Number <> 0 then
		strMessage = "URL に誤りがあります"
		Exit Function
	end if
	on error goto 0

	Http.Send

	Set Stream = CreateObject("ADODB.Stream")
	Stream.Open
	Stream.Type = adTypeBinary
	Stream.Write Http.responseBody
	Stream.SaveToFile _
		Server.MapPath("download.dat"), adSaveCreateOverWrite
	Stream.Close

	strMessage = "ダウンロードが完了しました"

end function

' **********************************************************
' CONTROL
' **********************************************************
	if Request.Form( "send" ) = "ダウンロード" then
		Call Download()
	end if

%>

<!-- **********************************************************
  VIEW
*********************************************************** -->
<FORM method=POST>
ダウンロードするファイルの URL
<INPUT
	size=100
	type=text
	name=url
	value="<%= Request.Form( "url" ) %>">
<INPUT type=submit name=send value="ダウンロード">
<HR>
<%= strMessage %>
</FORM>
  

responseBody は、バイト配列を戻します。
( ADO のストリームオブジェクトを adTypeBinary でバイナリ対応にしているのでそのままファイルを書き込みます )



  Web ページの中継表示

  
<%
Response.ExpiresAbsolute=#May 31,2000 23:59:59#

Dim strMessage

' **********************************************************
' MODEL
' **********************************************************
function DispPage()

	if Trim( Request.Form("url") ) = "" then
		strMessage = "URL を入力して下さい"
		Exit Function
	end if

	Set Http = Server.CreateObject("Msxml2.ServerXMLHTTP")
	on error resume next
	Call Http.Open("GET", Request.Form("url"), False )
	if Err.Number <> 0 then
		strMessage = "URL に誤りがあります"
		Exit Function
	end if
	on error goto 0

	Http.Send

	Response.BinaryWrite Http.responseBody
	Response.End

end function

' **********************************************************
' CONTROL
' **********************************************************
	if Request.Form( "send" ) = "表示" then
		Call DispPage()
	end if

%>

<!-- **********************************************************
  VIEW
*********************************************************** -->
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=Shift_JIS">
</HEAD>
<FORM method=POST>
表示するWebページの URL
<INPUT
	size=100
	type=text
	name=url
	value="<%= Request.Form( "url" ) %>">
<INPUT type=submit name=send value="表示">
<HR>
<%= strMessage %>
</FORM>
  

Web ファイルダウンロードと違い、表示目的なので HTTP ヘッダへのキャラクタセットを行わずに、
入力画面のみメタタグでキャラクタセットを指定しています

BinaryWrite を使用してバイト配列をそのまま送信しています










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





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

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ