Re: sample file upload

Brandon Long <[email protected]> Mon, 11 Aug 2008 14:57:14 -0700
Newsgroups gmane.text.clearsilver.general
Organization Fiction L Networks
Message-ID <20080811215714.GA13118@bl1>
Yes, its possible.  The code is obviously similar to the python from
before.  The key point is, your HTML code must have a FORM element which
has the extra parameter for using multipart/form-data:

<form name=upload method=POST enctype="multipart/form-data">
<input type=file name=myfile>
</form>

Then, as normal, you need to call cgi_init and cgi_parse.  That will
process the upload, placing the data in a file on disk in the /var/tmp
directory.  You can change the directory the upload data goes into by
setting the HDF variable Config.Upload.TmpDir to something else.

Normally, the file is "unlinked" after being opened.  This means the
file is only available to your process, and will be deleted as soon as
the process is ended.  You can set Config.Upload.Unlink to 0 to stop
this behavior.  If the files aren't automatically unlinked at creation
time, they will be cleaned up when cgi_destroy runs, but that won't
happen if your process dies prematurely, of course.

Normally, you can access the data in the file by using the
cgi_filehandle() function.  This will return a FILE*, which you can use
to read the file on disk.  Arguments are your CGI and also the form
element/query name, ie in the above example, that would be "myfile".

You can also get more information about the file, if its available.  If
the browser provided a Content-Type with the upload, it'll be in the
Query.myfile.Type variable.  The filename on the client as provided by
the browser will be in the Query.myfile variable.  If you are not in
unlink mode, the filename on disk will be in Query.myfile.FileName.  The
latter could be used to say link the file to another place on disk, for
instance.

You can get information during the upload (in cgi_parse) by setting up a
callback via cgi_register_parse_cb.  There's a lot more information
about all of this in the comments in cgi/cgi.h

Brandon

On 08/11/08 maxsimon1980 uttered the following other thing:
> Thanks for your response,
> but i dont want to use python or any other script.
> I can use clearsilver C API with cgi application .i just want to know
> whether it is possible or not.If any one have a sample application
> please upload 
> 
> Regards
> Max
> 
> 
> --- In [email protected], Brandon Long <blong@...> wrote:
> >
> > Do you have a perferred language?
> > 
> > Attached is a test python one I had lying around.  upload.py is a dirt
> > simple one, upload2.py uses a callback so you get information as the
> > uplaod is going on.
> > 
> > Brandon
> > 
> > On 08/08/08 maxsimon1980 uttered the following other thing:
> > > hello every one,
> > > 
> > > can anyone send me a sample code for file up load.
> > > please send me the code for cgi and html part.
> > > 
> > > Regards
> > > Max
> > > 
> > > 
> > > ------------------------------------
> > > 
> > > Yahoo! Groups Links
> > > 
> > > 
> > > 
> > -- 
> >  "A fundamental law:  no matter how good you are, someone somewhere 
> >   believes that you're going to Hell." -- Andrew C. Bulhak
> >                                            http://www.fiction.net/blong/
> > 
> > #!/usr/local/bin/python2.2
> > import neo_cgi
> > import sys, os
> > import traceback
> > 
> > def exceptionString():
> >   import StringIO
> > 
> >   ## get the traceback message  
> >   sfp = StringIO.StringIO()
> >   traceback.print_exc(file=sfp)
> >   exception = sfp.getvalue()
> >   sfp.close()
> > 
> >   return exception
> > 
> > class Page:
> >   def __init__(self):
> >     self.ncgi = neo_cgi.CGI()
> >     self.ncgi.setUploadCB(None, self.upload_cb)
> >     try:
> >       os.unlink('/tmp/cstest.txt')
> >     except os.error:
> >       pass
> >     try:
> >       os.unlink('/tmp/file')
> >     except os.error:
> >       pass
> >     self.ncgi.parse()
> > 
> >   def upload_cb(self, foo, nread, length):
> >     f = open('/tmp/cstest.txt', 'a')
> >     f.write("Uploaded %d out of %d\n" % (nread, length))
> >     f.close()
> >     return 0
> > 
> >   def display(self, file):
> >     if self.ncgi.hdf.getValue("Query.file", ""):
> >       try:
> >         fp = self.ncgi.filehandle("file")
> >         data = fp.read()
> >         f = open("/tmp/file", "w")
> >         f.write(data)
> > 
> >       except neo_cgi.CGIFinished:
> >         return
> >       except Exception, Reason:
> >         sys.stderr.write("Python Exception: %s\n" % (str(repr(Reason))))
> >         s = neo_cgi.text2html("Python Exception: %s" %
> exceptionString())
> >         self.ncgi.error(s)
> >     self.ncgi.display(file)
> >     sys.stdout.write('<pre>%s</pre>' % 
> >         neo_cgi.htmlEscape(self.ncgi.hdf.dump()))
> > 
> > p = Page()
> > p.display("hello.cst")
> > 
> > <html>
> > <title>Test upload</title>
> > <body>
> > <form name=upload method=POST enctype="multipart/form-data">
> > <input type=file name=file>
> > <input type=submit name=Action.Upload value="Upload">
> > </body>
> > </html>
> > 
> > #!/usr/bin/env python
> > 
> > import sys, os, traceback, string
> > import neo_cgi
> > 
> > def log (s):
> >   sys.stderr.write("CGI: %s\n" % s)
> > 
> > def exceptionString():
> >   import StringIO
> > 
> >   ## get the traceback message  
> >   sfp = StringIO.StringIO()
> >   traceback.print_exc(file=sfp)
> >   exception = sfp.getvalue()
> >   sfp.close()
> > 
> >   return exception
> > 
> > def main (argv, environ):
> >   # log ("starting")
> >   cgi = neo_cgi.CGI("")
> > 
> >   try:
> >     fp = cgi.filehandle("file")
> >     print "Content-Type: text/plain\r\n\r\n"
> >     data = fp.read()
> >     print data
> > 
> >     f = open("/tmp/file", "w")
> >     f.write(data)
> > 
> >   except neo_cgi.CGIFinished:
> >     return
> >   except Exception, Reason:
> >     log ("Python Exception: %s" % (str(repr(Reason))))
> >     s = neo_cgi.text2html("Python Exception: %s" % exceptionString())
> >     cgi.error (s)
> > 
> > if __name__ == "__main__":
> >   main (sys.argv, os.environ)
> >
> 
> 
> 
> ------------------------------------
> 
> Yahoo! Groups Links
> 
> 
> 
-- 
  "One of the main causes of the fall of the Roman Empire was that,
   lacking zero, they had no way to indicate successful termination of
   their C Programs." -- Robert Firth
                                           http://www.fiction.net/blong/