How to download a file using HTTP using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Created: rathodavinash (28 Jun 2007)
Last edited: hamishwillee (30 May 2013)

The following article shows how to use the HTTP Client to download and store a file on the device.

Before the transaction

The file download can be initiated using the GET Method of the HTTP Client. But to store the file being downloaded the following needs to be done: (in the get method itself)

  • Create a file on the device.
_LIT(KXMLFilePath, "c:\\mytempfiles\\");
TInt err=iRFileObj.Open(iFsSession,iCurrentFileName,EFileWrite);
if (err==KErrNotFound) // file does not exist - create it

once created the file contents are appended as the download happens.

  • Initiate the transaction for the file to download

A file download can happen as a GET to a particular URL, e.g. http://www.foo.com/myfile.xml, here the file to download is myfile.xml. This file will downloaded and stored as default.xml in our example.

During the transaction

Once the GET has started use the following code in MHFRunL to store the file

case THTTPEvent::EGotResponseBodyData:
// Get text of response body
MHTTPDataSupplier* dataSupplier = aTransaction.Response().Body();
TPtrC8 ptr;
TInt aPos=0;
iRFileObj.Seek(ESeekCurrent, aPos);
iRFileObj.Write(ptr); //save the file being downloaded
HBufC* buf = HBufC::NewLC(ptr.Length());
if (!iResponseBuffer)
iResponseBuffer = buf->AllocL();
iResponseBuffer = iResponseBuffer->ReAllocL(iResponseBuffer->Length()+buf->Length());
// Release buf
// Release the body data

Now the file has been stored and can be accessed at "C:\\mytempfiles\\default.xml"

This page was last modified on 30 May 2013, at 07:33.
52 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.