I want to upload a binary file to a http server using http post method. I have used HttpClientExample as template and modified some functions. It can connect to the internet using an internet access point and when i check the logs, it shows like :
Sent : 30 Kb
Recieved : 6 Kb
meaning that it has posted the file.
I write the incoming data from the http server to a html file to get the server result. It says "No file was uploaded". And it does not upload the file.
I could'nt figure out where the problem is.
Please help me.
Thank you all
The modified HttpClientExample functions are :
And the observer functions are :Code:_LIT8(KUserAgent, "SimpleClient 1.0"); _LIT8(KAccept, "*/*"); _LIT8(KPostContentType, "multipart/form-data; boundary=AaB03x"); _LIT8(KMimeType, "multipart/form-data; boundary=AaB03x"); _LIT8(KDataStart,"--AaB03x"); _LIT8(KCrlf,"\r\n"); _LIT8(KContent,"Content-Disposition: form-data; name='userfile'; filename='"); _LIT8(KFileCompletion,"'"); _LIT(KContent2,"Content-Type: image/gif"); _LIT(KContent3,"Content-Transfer-Encoding: binary"); _LIT8(KDataEnd,"--AaB03x--"); const TInt KStatustextBufferSize = 32; const TInt KInfotextBufferSize = 64; const TInt KURIBufferSize = 128; TUint32 CHttpClientEngine::GetGprsAccessPoint() { // open the IAP communications database CCommsDatabase* commDB = CCommsDatabase::NewL(EDatabaseTypeIAP); CleanupStack::PushL(commDB); CCommsDbTableView* commDbTableView = commDB->OpenTableLC(TPtrC(IAP)); CCommsDbConnectionPrefTableView* commDBView = commDB->OpenConnectionPrefTableInRankOrderLC(ECommDbConnectionDirectionOutgoing); TInt err; TBuf<128> iapName; TUint32 iapID ; // Point to the first entry if (commDBView->GotoFirstRecord() == KErrNone) { do { commDbTableView->ReadTextL(TPtrC(COMMDB_NAME), iapName) ; if(iapName.Compare(_L("Operator Internet")) == 0) { commDbTableView->ReadUintL(TPtrC(COMMDB_ID), iapID) ; break; } err = commDbTableView->GotoNextRecord() ; } while (err == KErrNone) ; } CleanupStack::PopAndDestroy(commDBView); CleanupStack::PopAndDestroy(commDbTableView); CleanupStack::PopAndDestroy(commDB); return iapID ; } void CHttpClientEngine::SetupConnectionL() { TInt bearerFilter = EApBearerTypeAllBearers; TInt currentProfileId; // Check whether we are offline or online iRepository->Get(KProEngActiveProfile, currentProfileId); // Close the connection only if // a) this is not the first time and // b) the profile has changed and // c) either the previous or the current profile is Offline (5 = Offline) if (iPrevProfileId != -1 && iPrevProfileId != currentProfileId && (iPrevProfileId == 5 || currentProfileId == 5)) { // Close and uninitialize iConnectionSetupDone = EFalse; iSession.Close(); iConnection.Close(); iSocketServ.Close(); } // Save current profile id iPrevProfileId = currentProfileId; // Try to find an existing connection. If connection has not been set up, // iConnection is not initialized and FindExistingConnection() fails. // Thus, in that case, finding must not be carried out. if (iConnectionSetupDone && !FindExistingConnection()) { iConnectionSetupDone = EFalse; } if (iConnectionSetupDone) { // Connection setup is done return; } // Open RHTTPSession with default protocol ("HTTP/TCP") iSession.OpenL(); // Install this class as the callback for authentication requests. When // page requires authentication the framework calls GetCredentialsL to get // user name and password. InstallAuthenticationL(iSession); // In offline, only WLAN connections are available if (currentProfileId == 5) { bearerFilter = EApBearerTypeWLAN; } iSelectedIap = GetGprsAccessPoint() ; // IAP Selected // Open socket server and start the connection User::LeaveIfError(iSocketServ.Connect()); User::LeaveIfError(iConnection.Open(iSocketServ)); // Now we have the iap Id. Use it to connect for the connection TCommDbConnPref connectPref; // Setup preferences connectPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt); // Sets the CommDb ID of the IAP to use for this connection connectPref.SetIapId(iSelectedIap); // Start connection User::LeaveIfError(iConnection.Start(connectPref)); // Set the sessions connection info... RStringPool strPool = iSession.StringPool(); RHTTPConnectionInfo connInfo = iSession.ConnectionInfo(); // ...to use our socket server and connection connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), THTTPHdrVal (iSocketServ.Handle()) ); // ...to use our connection connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable() ), THTTPHdrVal (REINTERPRET_CAST(TInt, &(iConnection))) ); iConnectionSetupDone = ETrue; } void CHttpClientEngine::IssueHTTPPostL(const TDesC8& aUri, const TDesC8& aContentType, const TDesC& aFilePath, const TDesC8& aFileName) { // Parse string to URI TUriParser8 uri ; uri.Parse(aUri) ; // Copy data to be posted into member variable; iPostData is used later in // methods inherited from MHTTPDataSupplier. delete iPostData ; iPostData = 0 ; // Read file data RFs aFs ; User::LeaveIfError(aFs.Connect()) ; CleanupClosePushL(aFs) ; RFile aFile ; User::LeaveIfError(aFile.Open(aFs, aFilePath, EFileShareAny)) ; TInt aSize ; User::LeaveIfError(aFile.Size(aSize)) ; iPostDataImage = HBufC8::NewL(aSize) ; TPtr8 aPtr = iPostDataImage->Des() ; //Obviously we have to read in the data to the iPostDataImage (ray) aFile.Read(0, aPtr, aSize); aFile.Close() ; aFs.Close() ; CleanupStack::PopAndDestroy(&aFs) ; // Read file data end // Prepare packet iPostData = HBufC8::NewL(650+aPtr.Length()); TPtr8 iPostDataPtr = iPostData->Des(); iPostDataPtr.Zero(); iPostDataPtr.Append(KCrlf); iPostDataPtr.Append(KDataStart); iPostDataPtr.Append(KCrlf); iPostDataPtr.Append(KContent); iPostDataPtr.Append(aFileName); iPostDataPtr.Append(KFileCompletion); iPostDataPtr.Append(KCrlf); iPostDataPtr.Append(KContent2); iPostDataPtr.Append(KCrlf); iPostDataPtr.Append(KContent3); iPostDataPtr.Append(KCrlf); iPostDataPtr.Append(KCrlf); iPostDataPtr.Append(aPtr); //the file in binary iPostDataPtr.Append(KCrlf); iPostDataPtr.Append(KDataEnd); iPostDataPtr.Append(KCrlf); // Prepare packet end // Create HTTP connection TRAPD(err, SetupConnectionL()); // User didn't select an IAP if (err == KErrNotReady) { return; } // Get request method string for HTTP POST RStringF method = iSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable()); // Open transaction with previous method and parsed uri. This class will // receive transaction events in MHFRunL and MHFRunError. iTransaction = iSession.OpenTransactionL(uri, *this, method); // Set headers for request; user agent, accepted content type and body's // content type. RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection(); SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent); SetHeaderL(hdr, HTTP::EAccept, KAccept); SetHeaderL(hdr, HTTP::EContentType, aContentType); // Set this class as an data supplier. Inherited MHTTPDataSupplier methods // are called when framework needs to send body data. MHTTPDataSupplier* dataSupplier = this; iTransaction.Request().SetBody(*dataSupplier); // Submit the transaction. After this the framework will give transaction // events via MHFRunL and MHFRunError. iTransaction.SubmitL(); iRunning = ETrue; }
Code:void CMyAppUi::SendFile() { SendFileToServer(_L("C:\\Data\\test.pdf"), _L("test.pdf")) ; } void CMyAppUi::SendFileToServer(const TDesC& aFilePath, const TDesC& aFileName) { iHttpClientEngine->CancelTransaction(); // Query uri and data to post TBuf<KDefaultBufferSize> uri; TBuf8<KDefaultBufferSize> uri8; uri.Copy(_L("http://w17.easy-share.com/cgi-bin/upload.cgi")) ; uri.LowerCase(); uri8.Copy(uri); TBuf8<KMaxFileName> fileName8 ; fileName8.Copy(aFileName) ; // Start transaction TRAPD(err, iHttpClientEngine->IssueHTTPPostL(uri8, KPostContentType, aFilePath, fileName8)); // TODO: Error handling if (err) { } } void CMyAppUi::HttpClientBodyReceived(const TDesC8& aBodyData) { // Write the result string to a html file TBuf<256> aFile ; aFile.Copy(_L("C:\\Data\\result.html")) ; RFs fsSession ; User::LeaveIfError(fsSession.Connect()) ; RFile rFile ; TInt err = rFile.Open(fsSession, aFile, EFileStreamText | EFileWrite | EFileShareAny) ; if(err == KErrNotFound) // file does not exist { err = rFile.Create(fsSession, aFile, EFileStreamText | EFileWrite | EFileShareAny) ; } else { err = rFile.Replace(fsSession, aFile, EFileStreamText | EFileWrite | EFileShareAny) ; } TInt pos(0) ; rFile.Seek(ESeekEnd, pos) ; User::LeaveIfError(rFile.Write(aBodyData)) ; User::LeaveIfError(rFile.Flush()) ; rFile.Close() ; fsSession.Close(); }



.U are right i have not noticed the date of the post.That's my misttake.

