Upload images to a TG2 app with Dojo (Ajax style)

123 views
Skip to first unread message

Carl

unread,
Jul 27, 2009, 5:13:31 AM7/27/09
to TurboGears
Hi,
When I try to upload an image I get the filename as a string instead
of the file's content. I followed a few Dojo samples successfully, so
I wonder if TG strips the file content somewhere along the way.

The form is:

<form id="uploadForm" method="post" action="upload"
enctype="enctype="multipart/form-data">
<input type="file" name="image" value="">
<input type="button" id="uploadButton" value="Submit" />
</form>

The upload button is hooked up to an uploadImage() function:

var uploadButton = dojo.byId('uploadButton');
dojo.connect(uploadButton, 'onclick', 'uploadImage');

The uploadImage() function uses dojo.io.iframe.send (the only way to
upload a file) because xhrPost(0 can't upload files.

function uploadImage()
{
upload_url ="http://localhost:8080/upload_image";
var request = {
url: upload_url,
method: 'POST',
form: "uploadForm",
load: function(response, ioArgs)
{
console.log('success');
},
error: function(error)
{
console.log('failure');
},
};
// Must use io.iframe.send() to send files
dojo.io.iframe.send(request);
//dojo.xhrPost(request);
}

Here is my controller:

@expose()
def upload_image(self, **kw):
image = kw['image']
...

The image I get is just the filename I selected in the form.

Thanks, Carl

Carl

unread,
Jul 27, 2009, 5:41:29 AM7/27/09
to TurboGears
Ok, found it. I messed up the enctype. Should be: enctype="multipart/
form-data"

and NOT: enctype="enctype="multipart/form-data"

Carl

percious

unread,
Jul 27, 2009, 1:07:04 PM7/27/09
to TurboGears
cool!

Lukasz Szybalski

unread,
Aug 2, 2009, 8:07:59 PM8/2/09
to turbo...@googlegroups.com
Could you post more information.

Are you using dojo.fileUpload? or?

Can you select multiple files?

If you could post more information on how you got dojo and tg2 working.

Thanks,
Lucas
--
Using rsync. How to setup rsyncd.
http://lucasmanual.com/mywiki/rsync
OpenLdap - From start to finish.
http://lucasmanual.com/mywiki/OpenLdap
Reply all
Reply to author
Forward
0 new messages