[Zope] Upload files in Zope and store elsewhere

Juan Carlos Coruña jcoruna@umd.es
Wed, 22 Mar 2000 13:33:55 +0100


Marius Kjeldahl wrote:
>
> After some helpful advice, I have been successful in uploading files
> (images) to Zope. Now I am trying to stuff the uploaded images in a
> MySQL database, but am not able to access the image data.
>
> If I try to stuff the image data "raw" into the database by declaring a
> image:string parameter to the ZSQL Method and  <dtml-sqlvar image> in
> the ZSQL code I get a string like "<ZPublisher.HTTPRequest.FileUpload
> instance at 87956214>".
>
> Another approach I have tried was <dtml-var image fmt=sql-quote>, but
> this complains about the image being an instance and not a string.
>
> Another approach has been to access the actual "raw" image data using
> image.data in various combinations, but I have not gotten this to work
> anywhere (contrary to what it seems other people have managed to do),
> see this
>
> http://www.egroups.com/group/zope/26227.html?
>
> posting.
>
> Looking through Image.py I see quote a few methods for accessing the
> image as a "web" object through the use of URL. Internally, Image.py
> uses the data attribute extensively, but I have not been able to access
> this from within DTML (and I guess this has to do with data not being
> listed in either the properties or permission sections of Image.py).
>
> My final try will probably be to resort to using an External method to
> get access to the "full" Image.py object (including data), but I was
> hoping to avoid this.
>
> Has anybody done this (uploading in Zope and storing the data outside of
> Zope) successfully? If so, please give me a hint..
>
> Thanks,
>
> Marius Kjeldahl
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )

Hello Marius,

TI am a Newbie to Zope and this is the first time I post a Answer to the
mailing list. But I had the same problem and I hope I can help you.

After a lot of test I find out the following method to upload images to
MySQL and after that retrieve it in the web pages.

Write the folliwing ZSQL method:

       Arguments: N_
                  Titulo
                  Subtitulo
                  Texto
                  Foto

      insert into Novedad
        (N_, Titulo, Subtitulo, Texto, Foto)
      values (
        <dtml-sqlvar N_ type=string>,
        <dtml-sqlvar Titulo type=string>,
        <dtml-sqlvar Subtitulo type=string>,
        <dtml-sqlvar Texto type=string>,
        <dtml-sqlvar
"_.string.replace(REQUEST['Foto'].read(),'\010','\\010')" type=string>
      )

Note that I replace the octal char 010 by the string "\\010".
If you don´t escaape this char MySQL change it for the char 008. I don't
know why.
To upload the image I have the following DTML Method:

<form action="novedades" method="post" ENCTYPE="multipart/form-data">
  <input type="hidden" name="accion" value="insertar_novedad">
  Nº producto: <select name="N_">
    <dtml-in listar_N__producto>
      <option value="<dtml-var N_>"><dtml-var N_></option>
    </dtml-in>
  </select><p>
  Título: <textarea rows="2" cols="20" name="Titulo"></textarea><p>
  Subtítulo: <textarea rows="2" cols="20" name="Subtitulo"></textarea><p>
  Texto: <textarea rows="8" cols="40" name="Texto"></textarea><p>
  Foto: <input type="file" name="Foto"><p>
  <input type="submit" name="submit">
</form>

Note the the line "Foto: <input type=....".
And to retrieve the image I have this DTML Method:

Method name: image

    <dtml-in obtener_foto>
    <dtml-call "RESPONSE.setHeader('Content-Type', 'image/gif')">
    <dtml-call "RESPONSE.setHeader('Content-Length', _.len(Foto))">
    <dtml-var "_.string.replace(Foto,'\\010','\010')">
    </dtml-in>

Note that you must put the 5 lines in the same line (without newlines).

ZSQL Method name "obtener_foto":

      Arguments: N_

      select Foto
      from Novedad
      where
      <dtml-sqltest N_ type=string>

To insert the image in an dtml method:

<img src="image?N_=AC-M56E">

Hope this can help you.