views:

71

answers:

2

there is any sample showing how to use the blobstore api with ajax?

when i use forms works fine, but if i use jquery i don't know how to send the file and i get this error:

    blob_info = upload_files[0]
    IndexError: list index out of range

I have this code in javascript

function TestAjax()
{
 var nombre="Some random name";
 ajax={
    type: "POST",
    async:true,
    //dataType:"json",
    url:"{{upload_url}}",
    data:"nombreEstudio="+nombre,   
    error: function ()
    {
        alert("Some error");
        $("#buscando").html("");
    },            
    success: function()
             { alert("it's ok") }
 };             
 $.ajax(ajax);
}

When i use forms the file it's sended with a input tag (exactly like the doc's sample)

+2  A: 

Somehow you still need to get the multipart form data request to the server... so when you're using forms, I assume your <form> tag has something like this on it: enctype="multipart/form-data", right?

When you're just sending a "POST" via ajax, you're losing that multipart request, which is where your file is.

There are some jQuery "ajax file upload" plugins out there that may help you out.

Hope this helps!

** EDIT **

I guess one thing I can add to this is usually ajax file uploads (on the client) are implemented by either creating a hidden iframe, and using that iframe to submit a form, or using a form and posting it via JavaScript.

Matthew J Morrison
+2  A: 

I wrote a series of posts about exactly this.

Nick Johnson