Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 20:01, 14 October 2007.

How to upload a file to server with application/x-www-form-urlencoded

From Forum Nokia Wiki

It is simple to upload a file using application/x-www-form-urlencoded combined with base64. The only problem is that the uploading fails with files exceeding few 10th Kb.

Client source:

import httplib, urllib
import base64, os.path
 
 
def imageToURL( aPath ):
    # read the binary data of the picture
    data = open(aPath, 'rb').read()
    # encoded it to base64
    encodedData = base64.encodestring( data )
    headers = { "Content-type": "application/x-www-form-urlencoded",
                "Accept": "text/plain",
                }
 
    params = urllib.urlencode({ u'fileName': os.path.split(aPath)[1],
                                u'data':encodedData})
 
    conn = httplib.HTTPConnection( "yourURL.xxx" )
    conn.request( "POST", "/uploaderFolder/image_uploader.php", params, headers )
    response = conn.getresponse( )
    # returns "True" or "False" if failed
    print response.read( )
    # status for debugging
    print response.status
    conn.close( )
 
 
if __name__ == "__main__":
    imageToURL("yourImage.jpg")


Server source (image_uploader.php):

<?php
// In this example a directory "images" needs to be present on the same directory where
// image_uploader.php is, with the necessary rights for the script to write data inside
 
if(isset($_POST['fileName'])){
    $filename = $_POST['fileName'];
}
else{
    // if not: just stop here
    print "False";
    die();
}
 
if(isset($_POST['data'])){
    $encodedData = $_POST['data'];
    $data = base64_decode($encodedData);
}
else{
    // if not: just stop here
    print "False";
    die();
}
 
// full path for the image
$filepathname = 'images/'.$filename;
 
// write the file to the server into the images directory
$handle = fopen($filepathname, 'wb');
fputs($handle, $data, strlen($data));
fclose($handle);
 
// return
echo "True";
?>
Related Discussions
Thread Thread Starter Forum Replies Last Post
如何通过cmwap获取服务器数据 wsjwhm Symbian 7 2005-10-28 09:22
upload image to web server nowanda Browsing and Mark-ups 6 2004-06-25 07:34
S60-SymbC++-Reading entire SMS/MMS Message as a whole and passing them as parameter? periakaruppan Symbian Networking & Messaging 5 2005-09-06 10:51
image upload megalogika Digital Rights Management & Content Downloading 2 2003-06-05 09:09
JAD file download problem girishvmx General Browsing 2 2008-06-04 05:33
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZpythonQ
     qfnZtopicQUqfnTopicZseriesE5f60Q
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX