Handling File upload from html form in python -
i have simple html form , backend python handled cgi-bin. trying upload file through form , print contents of file in python script. html code:
<form id = "upload" enctype="multipart/form-data" action="/var/www/cgi-bin/test.py" method="post"> <div id = "table"> <table class = "center" border="1" cellpadding="10"> <tr><td style="height: 131px">product details</td> <td style="height: 131px">product name*: <input type="text" name="product" id="product" size="35" ><br /><br /> platform*: <input type="text" name="platform" id="platform" size="35" > </td></tr> <tr><td style="height: 131px">file upload</td> <td style="height: 131px"><p>upload host file: <input type="file" name="hostupload" /></p><br/> upload test file: <input type="file" name="testupload" /></p> </td></tr> <tr align="center"><td></td><td><input type = "submit" id="upload" value = "upload"/> </td></tr> </table> </div> </form>
my python code:
#!/usr/local/bin/python import os import commands import cgi, cgitb print "content-type: application/json" print form = cgi.fieldstorage() product = form['product'].value platform = form['platform'].value filedata = form['hostupload'] print product print platform if filedata.file: print filedata.file.read()
the product , platform printing alright. if add line filedata = form['hostupload'] , getting error:
file "/usr/local/lib/python2.7/cgi.py", line 541, in __getitem__, referer: [error] [client 10.1.1.212] raise keyerror, key,
but name "hostupload" correct in form. can please tell me going wrong?
Comments
Post a Comment