Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All file object attributes being sent along with actual xhr #12

Closed
daniel-ferguson opened this issue Dec 12, 2014 · 4 comments
Closed

Comments

@daniel-ferguson
Copy link

Hey, I've done a pretty minimal setup, with a single file attached to a model instance. It seems like there's a little bit of overhead going on in the upload xhr. Due to the flattening of attributes, I seem to end up with something like this being sent to the rails server.
The #<ActionDispatch::Http> stuff is just rails interpreting the raw bytes as a file (the bit that I want):

img.webkitRelativePath: "" 
img.lastModified: "1417019673000"
img.name: "Screenshot from 2014-11-26 16:34:33.png"
img.type: "image/png"
img.size: "33715"

img: #<ActionDispatch::Http::UploadedFile:0x007ffd68d50918 @tempfile=#<Tempfile:/tmp/RackMultipart20141212-8277-1uyxe66>, @original_filename="Screenshot from 2014-11-26 16:34:33.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"img\"; filename=\"Screenshot from 2014-11-26 16:34:33.png\"\r\nContent-Type: image/png\r\n">

The other attributes I'm just throwing away. I'll have a dig in and see if I can patch it up for my use case to not send file attributes, but was wondering if you want those attributes being sent or if it's just a side effect of accounting for nested objects in other attributes of the backbone model.

Cheers for a lovely plugin btw, perfect for my current use case.

@homeslicesolutions-zz
Copy link
Owner

Yea the metadata for the file I think can be very useful and I think should stay as part of the file object. I think others will need that metadata, so they can quickly interpret mime type or image size so that can validate quickly as it goes through the Backbone Model class. If you want just the blob sent, I guess that's just up to hacking it up in the model to get that to work.

Yea the flattening is required in order to go through the FormData API of the browsers which allows things like FileUploads whereas a normal Post doesn't allow Multipart since its just sending string data. This way the server reads it as multipart so it can stream the data through.

There is an idea i have about actually converting the Blob Object into a normal JS object then appending it into the JSON object as string then you can use a traditional Post. Not sure if it's a good idea, but I'll experiment with that. That way you don't have to worry about "unflattening" attributes as they get pushed through.

@daniel-ferguson
Copy link
Author

Sounds like a cool idea.
I've heard images can end up being some 30% bigger MB-wise in base64 (which json can transmit), but that may well be worth it simplicity wise, decoding it server side should be no problem.

The issue with flattening is that different web frameworks tend towards different defaults for how things should be flattened, rails uses object[field] notation in forms.

Anyway, just food for thought.
Thanks for writing this, found it very useful, I made some project-specific changes and all is good :)

@homeslicesolutions-zz
Copy link
Owner

Actually I think i take it back since doing it all in a JSON post it won't "stream" but try to push the string all at once. So no POST JSON, we need to use the FormData API.

@daniel-ferguson
Copy link
Author

Ah, didn't think of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants