why the base64 ? I want my files submitted as binaries, if possibile.....

On Wednesday, April 24, 2013 5:31:48 PM UTC+2, select wrote:
>
> in your client side js code use the js file api (this is for one file, but 
> multi file upload can be done too)
>
> $('#datafile-uploadfield').change(function() {
>     var upload_element = $(this)[0];
>     var file = upload_element.files[0];
>     if (file) {
>       var reader = new FileReader();
>       reader.readAsDataURL(file); //, "UTF-8");//TODO what encoding 
> should be parsed???
>       reader.onload = (function(theFile) {
>         return function(evt) {
>           $.ajax({
>             url: '{{=URL("datafile_create")}}',
>             data: {
>               data: evt.target.result,
>               name: theFile.name
>             },
>             type: 'POST',
>             dataType: 'json',
>             success: function(data) {
>               addDatafile(data);
>             }
>           });
>         };
>       })(file);
>       reader.onerror = function(evt) {
>         alert(":( oh noo, we could not read your file");
>       };
>       //++counter;
>     }
>   });
>
> in you controller
> def datafile_create():
>     splitcontents = request.vars.data.split(',')
>     import base64
>     file_content = base64.b64decode(splitcontents[1])
>     # create file like object
>     import StringIO
>     filelike_obj = StringIO.StringIO(file_content)
>     db_store = db.datafiles.file.store(filelike_obj, request.vars.name)
>     record_id = db.datafiles.insert(file=db_store)
>
>
> in you db.py
> db.define_table('datafiles',
>  Field('file', 'upload', requires=IS_NOT_EMPTY()),
>  )
>
> == PURE AWESOMENESS
>
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to