An ASP Component to Send Arbitary Large File from Server to Client | CodeGuru

An ASP Component to Send Arbitary Large File from Server to Client

#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; […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 1, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

#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 < (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;
}

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.