`

ASP Component to Send Arbitary Large File from Server to Client

阅读更多

This article was contributed by <!-- Author Email -->Shimon Pozzin.

Platform:

Windows NT 4.x SP4, ATL 2.x, MS Visual C++ 5.x, IIS 4.x

Purpose:

Demonstrate IResponse::BinaryWrite call from ATL component.

Description:

Writing some scripts in ASP (hey, this is not my job! :-) ) I came across the problem of sending the binary file from server to client.

How to use:

Attached project is full source code of the component, sample ASP file, sample HTM file and a sample GIF file. Of course, the file may be any binary file, but you should be careful about correct MIME type of the file being transferred. Source is free for distribution, modification etc.

Full annoying comments (aka how it was built):

IResponse::BinaryWrite() expects as a parameter a variant, containing safearray of type VT_ARRAY | VT_UI1 (array of unsigned char[s]) This project was build using standard ATL wizard with default options. Then, ASP object was inserted into the project (what else could you add to the project that must work in ASP script?!) Finally, one simple method, called GetFile() was added to the interface of the ASP object as follows:



#define RETURN { hr = E_FAIL; goto Cleanup; }	// don't kill me! Look at last examples of MS
						// under http://www.microsoft.com/data/xml
						// You can't catch me! :-)
STDMETHODIMP CGetFile::GetFile(VARIANT vFileName)
{
	_variant_t	vReturnBuffer;
	LPSAFEARRAY	psaFile;
	HANDLE		hFile;
	DWORD		dwSizeOfFile;
	DWORD		dwNumberOfBytesRead;
	BOOL		bResult;
	unsigned char	*pReturnBuffer = NULL;
	long		k;
	HRESULT		hr = S_OK;

	// Create file in this case only OPENS an existing file (or fails
	// if the file does not exist!)
	hFile = ::CreateFile(
			vFileName.bstrVal,		// name of the file
			GENERIC_READ,			// desired access
			FILE_SHARE_READ,		// shared access
			NULL,				// security attributes
			OPEN_EXISTING,			// creation disposition - open only if existing!
			FILE_FLAG_SEQUENTIAL_SCAN,	// flag attributes
			NULL );
		
	if( hFile == INVALID_HANDLE_VALUE )
	{
		return E_FAIL;
	}

	dwSizeOfFile = ::GetFileSize( hFile, NULL );
	if (dwSizeOfFile == 0xFFFFFFFF)
	{
		return E_FAIL;
	}

	try
	{
		pReturnBuffer = new unsigned char[dwSizeOfFile];
	}
	catch( std::bad_alloc& )
	{
		return E_FAIL;
	}

	// Get the binary content of the file
	bResult = ::ReadFile( hFile, pReturnBuffer, dwSizeOfFile, &dwNumberOfBytesRead, NULL );
	if( FALSE == bResult )
	{
		RETURN(E_FAIL);
	}

	psaFile = ::SafeArrayCreateVector( VT_UI1 /*unsigned char*/, 0, dwSizeOfFile );
		
	if( !psaFile )
	{
		RETURN(E_FAIL);
	}

	// Fill in the SAFEARRAY with the binary content of the file
	for( k = 0; k <!--webbot
bot="HTMLMarkup" startspan --><<!--webbot bot="HTMLMarkup" endspan -->(int) dwSizeOfFile; k++ )
	{
		if( FAILED(::SafeArrayPutElement( psaFile, &k, &pReturnBuffer[k] )) )
		{
			RETURN(E_FAIL);
		}
	}

	vReturnBuffer.vt = VT_ARRAY | VT_UI1;
	V_ARRAY(&vReturnBuffer) = psaFile;

	m_piResponse->BinaryWrite(vReturnBuffer);

Cleanup:

	if( pReturnBuffer )
		delete [] pReturnBuffer;

	return SUCCEEDED(hr) ? S_OK : E_FAIL;
}
from WWW.VCKBASE.COM

As you see, the idea is simple: to read the binary content of the file, to pack it into safearray and then to pass as a parameter to IResponse::BinaryWrite(). The process is pretty straightforward

Download source and demo project - 62 KB <!--comments-->



To test the demo project you have to put aspfile.asp, aspfile.dll and canflag.gif in the same directory, say, /test. Then register component (from command prompt issue: regsvr32 aspfile.dll.) Then create a virtual directory without read permissions pointing to /test. Create another virtual directory, called, say, /test1. Put aspfile.htm file into this directory. Original demo project is made so that all files are in the same virtual directory, so you have to change the path in aspfile.asp to real virtual directory where aspfile.asp file is located (e.g. "/test/aspfile.asp") That's it. Open a browser, type URL http://localhost/test1/aspfile.htm and observe wonderful canadian flag!
You may ask: "Why would I bother with script if I can simply point my URL to that path and to download the file like I did a million times?"
Well, sometimes you cannot. The main reason for that is execute-only permissions for the directory where the file is located. In this case you will not be able to get the file (browser will return an error).
This is when you should remember this handy component. You may send any binary file from server to client.
Response object of ASP contains a method called BinaryWrite. We have one problem, however. This method is not documented well. And there are no examples how to use it. This is why I decided to fill this gap.
分享到:
评论

相关推荐

    An ASP Component to Send Arbitary Large File from Server to

    An ASP Component to Send Arbitary Large File from Server to Client一个服务器端的组件用于从服务器传送文件到客户机(6KB)

    Visual C++ 编程资源大全(英文源码 ActiveX)

    02.zip ActiveX Control Tutorial ActiveX控件指南(11KB)&lt;END&gt;&lt;br&gt;3,03.zip An ASP Component to Send Arbitary Large File from Server to Client 一个服务器端的组件用于从服务器传送文件到客户机(6KB...

    Visual C++ 编程资源大全(英文源码 ATL)

    bankaccount1.zip Handling Interdependent Objects in Automation An Example of a Bank Server Object(53KB)&lt;END&gt;&lt;br&gt;57,aspfile.zip An ASP Component to Send Arbitary Large File from Server to Client...

    MITMf-master.zip

    - ```BeEFAutorun``` - Autoruns BeEF modules based on a client's OS or browser type - ```AppCachePoison``` - Perform app cache poisoning attacks - ```Ferret-NG``` - Transperently hijacks sessions - ``...

    php脚本资料电子书籍

    HotNews arbitary file inclusion Invision Power Board IP地址伪造漏洞 Invision Power Board SQL Injection Vulnerabil Invision Power Board v1.3 Final ssi.php SQL Injection Vulnerability IPB SQL Injection ...

    Foreign Library Interface-计算机科学

    Foreign Library Interfaceby Daniel AdlerDecember 13, 2011AbstractWe present an improved Foreign Function Interface (FFI) for R to call arbitary native func- tions without the need for C wrapper code. ...

    数据结构常用算法c++实现

    Arbitary Integer Linear congruential generator Maximum subarray problem Bit-Set Queue Stack Binary Heap Fibonacci Heap Priority Queue (list based) Bubble sort Selection sort Insertion sort Radix ...

    Regression with Gradient Descent:Regression with Gradient Descent; 所需系统模型的系数查找技术。-matlab开发

    梯度下降回归; 所需系统模型的系数查找技术我包含了不同的函数来使用下降梯度技术对数据进行建模对随机...在 Arbitary.m 文件中,我展示了可变学习率技术,以使用不同频率和偏移值的不同正弦波对随机生成的值进行建模

    任意偏振双色相干场高次谐波

    系统地研究了任意偏振双色相干场高次谐波(1ω与2ω或3ω),结果表明单个圆偏振光不能产生高次谐波,在适当的条件下,某些谐波可以部分或完全消失,而某些谐波可以得到加强,产生更高次谐波高转换效率的条件是双色线偏振场...

Global site tag (gtag.js) - Google Analytics