[Zope] Re: Unable to upload images from client to server

³¯§D§Ó u82019@ice.ntnu.edu.tw
Wed, 12 Jul 2000 14:54:01 +0800 (CST)


Our company, Cyberwork Solution Inc, uses Zope heavily to provide 
technicle solutions for our clients. Insert/Query image with MySQL was 
also a BIG problem for us, but now we've found one solution to solve this 
problem. Here is the solution we found, and hope it will help!!

Environment:
  Zope 2.1.6
  MySQL 3.22.30
  ZMySQLDA 1.1.3

[insert into MySQL]

--DTML--
<form method="post" action="py_sqlinsert"  ENCTYPE="multipart/form-data">
<input type=file name=file>
<input type=submit>
</form>

--External Method-- py_sqlinsert
from Products.ZMySQLDA.db import DB
from Products.ZMySQLDA import MySQL

###############################################################
# you should replace what id you want to set with 'cclljj' :->
###############################################################
def insertBLOB(self,file='',REQUEST=None):
  f = file.read()
  d = DB('csi@localhost db_account db_passwd')
  r = MySQL.escape(f)
  q_str = "insert into CSI (name,data) values ('cclljj','" +r+ "')"
  d.query(q_str)
  return "success"

[query from MySQL]

--DTML--
<hr>
<img src="py_sqlquery?key=cclljj">  
<!-- query the image with the key value 'cclljj' -->
<hr>

--External Method-- py_sqlquery
from Products.ZMySQLDA.db import DB
from Products.ZMySQLDA import MySQL

def queryBLOB(self, REQUEST, RESPONSE):
  key = REQUEST['key']
  d = DB('csi@localhost db_account db_passwd')
  q_str = "select data from CSI where name='"+key+"'"
  r = "Content-type: image/gif\n\n"
  k = d.query(q_str)
  k = k[1]
  k = k[0]
  k = k[0]
  RESPONSE.setHeader('Content-Type','image-gif')
  RESPONSE.setHeader('Content-Length',len(k))
  return k

In fact, we also found that there seems to be some thing wrong with 
ZMySQLDA 1.1.3. We strongly recommend you to patch your DA.py as follows:

def sql_quote__(self, v, escapes={
        '\\': '\\\\',
        '\"': '\\\"',
        '\'': '\\\'',
        '\0': '\\0',
        '\n': '\\n',
        '\t': '\\t',
        '\r': '\\r',
        '\b': '\\b',
        '\032': '\\Z',
        }):

Therefore when inserting into database, you can just use DTML method 
without external method as follows:

<dtml-let content="file.read()">
<dtml-var zsql_insert>

-----------------------------------------
Kevin Chen
Programmer,Cyberwork Solution Inc
4F-5,No.288,Kuangfu S.Rd.,Taipei,Taiwan
Email : cclljj@cyberworksolution.com
Tel : +886-2-87712220#2259
Fax : +886-2-87712222
http://www.cyberworksolution.com