[Zope] Uploading Images/Files To Database
Mike Doanh Tran
mtran@shufflemasterrd.com
Thu, 21 Nov 2002 13:32:37 -0700
Hi,
Can anyone show me how to upload an image or file from a form and insert it
into a database. I have the form and sql method figure out, but I don't know
how to read the file and upload it into the database. It's .read() something?
Here's what I got so far:
THE FORM:
<form name=attachment method=post ENCTYPE="multipart/form-data"
action="addAttachment">
<input type=hidden name=bug_id value="<dtml-var bug_id>">
To attach a file to bug ID#, place it in a
file on your local machine, and enter the path to that file here:<br>
<input type=file name="thedata" size=40>
<BR>
<b>File Name:</b>
<input type=text name="filename" size=40>
<P>
Please provide a one-line description of this attachment:<BR>
<input name="description" size=60>
<BR>
<b>What kind of file is this?</b>
<BR>
<select name="mimetype">
<option value="" SELECTED>Select File Type<option>
<option value="patch">Patch file (text/plain, diffs)
<option value="text/plain">Plain text (text/plain)
<option value="text/html">HTML source (text/html)
<option value="image/gif">GIF Image (image/gif)
<option value="image/jpeg">JPEG Image (image/jpeg)
<option value="image/png">PNG Image (image/png)
<option value="application/octet-stream">Binary file
(application/octet-stream)
<option value="other">Other
</select><BR>
<input type=button value="SUBMIT!" onClick="validateForm();">
</form>
addAttachment method:
<dtml-try>
<dtml-let login_name="AUTHENTICATED_USER.getUserName()">
<dtml-in "getRealName(login_name=login_name)">
<dtml-call
"insertAttachment(bug_id=bug_id,filename=filename,description=description,mimetype=mimetype,
ispatch=ispatch,submitter_id=userid,thedata=thedata)">
</dtml-in>
</dtml-let login_name>
<H3>Attachment Added</H3>
<LI>Your attachment was added to bug_id# <dtml-var bug_id>
<dtml-except>
<H3>Failed Add Attachment</H3>
<LI><h4>Error Type: <dtml-var error_type></h4></LI>
<LI><h4>Error Value: <dtml-var error_value></h4></LI>
</dtml-try>
ZSQL Method:
INSERT INTO attachments(bug_id, filename, description,
mimetype, ispatch, submitter_id, thedata)
VALUES(<dtml-sqlvar bug_id type="int">,
<dtml-sqlvar filename type="string">,
<dtml-sqlvar description type="string">,
<dtml-sqlvar mimetype type="string">,
<dtml-sqlvar ispatch type="int">,
<dtml-sqlvar submitter_id type="int">,
<dtml-sqlvar thedata type="string">)
The getRealName() is a lookup for the user's id#.
Thanks for any suggestions.
Mike