×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    39

    Uploading a file using http post problem

    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 :


    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;
    }
    And the observer functions are :


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

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    39

    Re: Uploading a file using http post problem

    No idea?

    Champs?

    I need your help.

  3. #3
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,166

    Re: Uploading a file using http post problem

    maybe you could check if you are actually handling it correctly at the server side.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    39

    Re: Uploading a file using http post problem

    Hi yucca,

    Thanks for reply.

    I have checked handling the file at the server side. Actually I have also a form to upload a file via web interface. I can upload my file using the form. But i can not upload it using this http post method.

    I have realized that when I don't point a file (I use null string) in form, I got the same error :

    "No file was uploaded"

    But, for my http post, as I have mentioned, when I checked the logs in the phone, I saw that lines :

    Sent: 30 Kb
    Receieved : 6 Kb

    for a test file whose size is 28 Kb.

    Thank you so much again. Any help will be appriciated.

    Regards.

  5. #5
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,166

    Re: Uploading a file using http post problem

    basically can you log all stuff into a file in your server side to see what is getting sent, with quick look your posting code looked quite ok.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    39

    Re: Uploading a file using http post problem

    Hi yucca,

    You are right. I have written the log in the server side. On the server side, I have a simple form and php script to process the posted file.

    When I use the form to upload a file, the php script accepts my operation as "http post" request and processes the file.

    But when I use this code to upload a file, the php script does not accept my operation as "http post" request. It says that "this is a file attack".

    I don't know how to make a server accept this code as a "http post request".

    Any change needed for the code?

    Thanks.

    Regards.

  7. #7
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    Re: Uploading a file using http post problem

    i see no reply for this problem as specified by the mynick1000 please help i also want to upload the file using HTTP POST

  8. #8
    Super Contributor
    Join Date
    Jul 2007
    Posts
    596

    Re: Uploading a file using http post problem

    Hi mynick1000,

    u have told abt the results
    Sent : 30 Kb
    Recieved : 6 Kb

    Can u please confirmed me by cross checking the status codes at in MHFRunL() functions . Are u getting the right status codes there.

    if it is the right code, as told by yucca it might be a pb at ur server side code.

  9. #9
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,684

    Re: Uploading a file using http post problem

    Kishore84: that happened more than half years ago, just lokesh_kumar_s has revived the thread.

    Lokesh_kumar_s: that is probably a server-side issue, check php forums, and also try Google. After the very first search you will already get an idea for what to check in your php code, and what configuration directive you can try.

  10. #10
    Super Contributor
    Join Date
    Jul 2007
    Posts
    596

    Re: Uploading a file using http post problem

    Quote Originally Posted by wizard_hu_ View Post
    Kishore84: that happened more than half years ago, just lokesh_kumar_s has revived the thread.

    Lokesh_kumar_s: that is probably a server-side issue, check php forums, and also try Google. After the very first search you will already get an idea for what to check in your php code, and what configuration directive you can try.
    Oops .U are right i have not noticed the date of the post.That's my misttake.

    Agree to wizrd it could be ur server side pb if u cross checked it properly as i have said in previous post.

    Thanks wizard again..

  11. #11
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    Re: Uploading a file using http post problem

    hi mynick1000 can u please tell me how do u get the content of image at the client side and how do u create the image from that image please help.

    i used conteny-type as KContentType,"application/x-www-form-urlencoded"
    and posting photo=[some contents]
    so i am getting those contents in $_POST['photo'] and then writing it into file called ccc.jpg but its size appears to be 111 bytes but i posted the content of 40KB so image is not visible it tells me no preview available.
    please help. thanks

  12. #12
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,684

    Re: Uploading a file using http post problem

    I am still not interested in your php problem. But if you have 111 bytes, you should consider checking what they are.

  13. #13
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    Re: Uploading a file using http post problem

    its ok dont tell any thing about php simply tell am i doing correctly see
    for Content type header if i put it as application/x-www-form-urlencoded then i am able to get the POST data i.e $_POST['photo'] if i use content type as image/jpeg i cannot get any data inside $_POST['photo'] why?

  14. #14
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    Re: Uploading a file using http post problem

    mynick1000 has the solution he has uploaded image content in POST request and he knows how to write those contents into the .jpg file using any sirver side script.
    i dont care if it is php or asp or any other scripting language i just want to know am i correct. if i contact mynick1000 it will give me good and quick solution

  15. #15
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India,Karnataka
    Posts
    432

    Re: Uploading a file using http post problem

    Thanks all i got it

Similar Threads

  1. Nokia N70 - HTTP POST problem
    By heuven in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2007-04-13, 15:48
  2. HTTP POST Problem with SDK's
    By tonyhabayeb in forum Mobile Java Tools & SDKs
    Replies: 11
    Last Post: 2006-08-10, 23:14
  3. HTTP POST Problem with SDK's
    By tonyhabayeb in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2006-06-16, 13:41
  4. Http post problem with emulator
    By robbin123 in forum Symbian
    Replies: 0
    Last Post: 2006-06-13, 21:37
  5. J2ME HTTP Post problem
    By gilfr in forum Mobile Java General
    Replies: 2
    Last Post: 2003-06-11, 08:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×