How to upload content for type file?

49 views
Skip to first unread message

sziegler

unread,
Mar 13, 2012, 6:25:19 PM3/13/12
to spring...@googlegroups.com
Hi everyone,

I'm currently trying to add some attachments to me objects. It's easy for me to create the springpad object "File" itself, but how can I put the (binary) data to that object? Seems like I'm really stupid to ask this question, but I really don't know the answer. Should the binary content be attached to the POST-request via the header? Can you please give me a simple to understand example? That would be awesome!

Cheers,
Sven

sziegler

unread,
Mar 15, 2012, 2:41:41 PM3/15/12
to spring...@googlegroups.com
Hi,

any help would be really much appreciated!

Cheers,
Sven

Jason Horman

unread,
Mar 15, 2012, 2:55:00 PM3/15/12
to spring...@googlegroups.com
There is an api

POST /users/{username}/blocks/{blockUuid}/files

The POST data is the binary.

There is a parameter "filename" for the name to save the file as on our servers (for downloading)

The blockUuid is the uuid of the File object you created. On the server the file object will have a "key" field set on it, which is the storage key for finding the file again.

If you are trying to attach a file to an existing object like a note, the uuid can be that of the note, and a sub File object will be created and attached.

sziegler

unread,
Mar 15, 2012, 3:00:11 PM3/15/12
to spring...@googlegroups.com
Hi and thanks for your answer!

Your last sentence confuses me a bit. Does it mean that using the api url you mentioned and using the uuid of an existing object (for example an note where I want to attach the file) then I must not create a file object by myself and it would be created automatically this way ?

Cheers,
Sven

Jason Horman

unread,
Mar 15, 2012, 3:01:55 PM3/15/12
to spring...@googlegroups.com
Yeah, exactly. Sort of whether you are creating a top level file or an attachment.

sziegler

unread,
Mar 15, 2012, 3:08:17 PM3/15/12
to spring...@googlegroups.com
Sounds great. Thanks a lot for your help. I will have a look later today and try to implement it.

Thanks again.

Cheers,
Sven

Jason Horman

unread,
Mar 15, 2012, 3:10:45 PM3/15/12
to spring...@googlegroups.com
Let me know how it works for you. We do have some bigger api changes coming. I am working on new docs too, and a warning for what is coming. One thing you should do is set

X-Spring-Api-Version: 6

as a header. We are moving to api version 7 soon, but 6 should give you backwards compatibility.

sziegler

unread,
Mar 15, 2012, 3:13:02 PM3/15/12
to spring...@googlegroups.com
I will let you know if it works for me!

Thanks for the hint regarding the api version key for the header. That is already implemented by me, thanks to Brittni and Dan. :)

Cheers,
Sven

sziegler

unread,
Mar 15, 2012, 3:59:23 PM3/15/12
to spring...@googlegroups.com
It nearly works for me. Nearly for that reason, because the filename is missing even when I set it via the api url you gave me. 

But I can remember that somebody else also had this issue in the past. And I think he had to set the attribute filename by himself with the next command for the newly created file-object. Can you verify that?

Cheers,
Sven

sziegler

unread,
Mar 15, 2012, 5:59:41 PM3/15/12
to spring...@googlegroups.com
After searching this developer group I found the solution for the workaround and it finally works.

Thanks again!

Cheers,
Sven

sziegler

unread,
Mar 19, 2012, 2:51:20 PM3/19/12
to spring...@googlegroups.com
I still have some problems uploading a file. Every time I upload a file I get the following error message: Error uploading, content length doesn't match bytes uploaded length

It does not matter if I upload a html file or a pdf document. 

Could you please help me again?

Cheers,
Sven

jhorman

unread,
Mar 20, 2012, 3:28:20 PM3/20/12
to Springpad Developers
Are you sending a "Content-Length" header, standard http? If so, it
has to match the byte length of your upload.

Back to your filename issue, are you sure you tried to do /users/me/
blocks/uuid/files?filename=test.txt



On Mar 19, 2:51 pm, sziegler <dunkings...@googlemail.com> wrote:
> I still have some problems uploading a file. Every time I upload a file I
> get the following error message: *Error uploading, content length doesn't
> match bytes uploaded length*
> *
> *

sziegler

unread,
Mar 21, 2012, 3:53:12 AM3/21/12
to spring...@googlegroups.com
I'm sending a "Content-Length" header with http! And I send the same amount as displayed in my filebrowser. 

I'm now using http, just switched from https, for uploading the file content. Is that right? But now I do no more get the error message mentioned a few posts before, but a http error code 413...  Do you have any ideas?

The filename problem is currently not existing.

Cheers,
Sven

sziegler

unread,
Mar 21, 2012, 2:36:47 PM3/21/12
to spring...@googlegroups.com
One more thought: I upload the file as binary base64 encoded, is that what your server expects? Or could that be a problem, too?

Jason Horman

unread,
Mar 21, 2012, 2:54:14 PM3/21/12
to spring...@googlegroups.com
Oh, yeah, don't do that. We literally just take the bytes and save them up to Amazon S3, so people downloading those files won't be seeing them properly.

sziegler

unread,
Mar 21, 2012, 3:00:00 PM3/21/12
to spring...@googlegroups.com
Okay, but how do you expect the binary data? I have to encode it, to send it via XMLHttpRequest to you. If I would not encode it, the bytes would be send in UTF-8 format. If I upload it without encoding, then the files are uploaded fine and have the correct size, filename and description, but I'm only able to open the files, because the content seems to be damaged.

sziegler

unread,
Mar 21, 2012, 3:01:18 PM3/21/12
to spring...@googlegroups.com
Sorry, I meant when I am sending in UTF-8 I'm NOT able to open the files!

Jason Horman

unread,
Mar 21, 2012, 3:18:19 PM3/21/12
to spring...@googlegroups.com
I have not tried doing this from javascript myself, but I do see support for base64. There are a few tricks to make it work though. You can see it happening if you drag a file onto a canvas on the web in a notebook.

The URL is

http://springpadit.com/api/users/me/blocks/f7325516-ff1e-452b-8071-fffceca3a23/files?filename=something.pdf&encoding=base64

Payload:
data:base64,SW5kZXg6IC4uLy4uLy4uL3NyYy9tYWluL2phdmEvY29tL3NwcmluZy9zdGFjay9jbGllbnQvc3RvcmFnZS9Mb2NhbFNlYXJjaC5qYXZhCklERUEgYWRkaXRpb25hbCBpbmZvOgpTdWJzeXN0ZW06IGNvbS5pbnRlbGxp

So the trick is both to do a query param of "encoding=base64" and prefix the post data with "data:base64,"

If you have further trouble maybe we should just hop on an IRC channel or something. (not that I have one setup at the moment, but we could)

sziegler

unread,
Mar 21, 2012, 3:38:06 PM3/21/12
to spring...@googlegroups.com

Jason, I don't know what to say but I was finally able to upload my first pdf via api-call! The "encoding=base64" did the trick for me. Man, you are AWESOME!

Thank you very much! :))))

I really appreciate all the time you spent in helping me with my problems. That is really GREAT support! Thanks again!

Cheers and best wishes,
Sven

Reply all
Reply to author
Forward
0 new messages