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; // 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



Comments

  • I have source code.

    Posted by Legacy on 09/10/2002 12:00am

    Originally posted by: CSConcordia

    send me mail, I give you the source code.

    Reply
  • Where is the Source Code????

    Posted by Legacy on 08/06/2002 12:00am

    Originally posted by: Djay

    Hi, 
    
    The artice was an interesting one... But I was not able to download the Source Code of ur article, Do tell me or send me the Source code.

    Reply
  • want to code

    Posted by Legacy on 07/17/2002 12:00am

    Originally posted by: Zafar Asghar

    respected sir
    i am doing my project of asp and that is "online jobs" . i am confuse what to do please help me. i am waiting for your reply plz guide me.i am waiting for you.

    ok bye

    Reply
  • Where is the Source Code

    Posted by Legacy on 03/06/2002 12:00am

    Originally posted by: Dion

    Hello,
    I am trying to do the same thing but when I click on the link to download the zip file, the page could not be found.

    Can you send me the Zip file with the source and project?

    Thanks

    Reply
  • Help me~~~

    Posted by Legacy on 02/12/2002 12:00am

    Originally posted by: James

    This source does not work properly...
    
    

    ==================================================
    Microsoft VBScript RunTime error '800a01fb'

    Exception : 'GetFile'

    ==================================================

    why..?

    please help me...

    Reply
  • Where is the source code?

    Posted by Legacy on 12/14/2001 12:00am

    Originally posted by: Carlos

    The link is bad!

    Reply
  • source code

    Posted by Legacy on 11/06/2001 12:00am

    Originally posted by: dennis

    source code

    Reply
  • perfect .

    Posted by Legacy on 10/20/2001 12:00am

    Originally posted by: abdallah

    perfect
    

    Reply
  • How do I register the IResponse class?

    Posted by Legacy on 09/06/2001 12:00am

    Originally posted by: doug w

    Apparently the IResponse object is intrinsic. The object is created and destroyed upon opening and closing each ASP page. An example of how to initialize this type of object can be found in the MSDN Library by searching for "Developing Active Server Components with ATL". It took about 2 hours of searching, and about 30 minutes of reading, but the document is very straight forward. I hope this helps anyone else that decides to use this function.

    dougw

    Reply
  • Source code please?

    Posted by Legacy on 09/06/2001 12:00am

    Originally posted by: doug w

    Fix the source code link please.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: August 27, 2015 With the cloud enabling companies to spin up servers and stand up data stores more quickly, mobile apps can be created faster, reducing the time-to-value. But three major obstacles stand in the way for many organizations: The backlog of app project requests confronting every enterprise regardless of their internal app development capabilities Finding and employing better, faster tools to speed and simplify the process of developing those apps. The emergence of …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date