You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Curt Arnold <ca...@apache.org> on 2009/04/19 03:38:26 UTC
Creating attachments from HTML's INPUT type="file"
I've been developing a custom browser-hosted application using CouchDB
for a client and have so far been able to avoid introducing an
intermediary between the browser and CouchDB (aka a CouchApp).
However, I have run into a stumbling block trying to support uploading
attachments to items from the browser.
The only mechanism that seems to provide access to the local
filesystem is HTML's file-select control and does not appear to
provide a mechanism to control the request sent to the action URL so
that it could conform to the CouchDB API.
Would it be practical or desirable to introduce an endpoint that could
directly process the request that would be sent from such a form?
Would anyone be interested in taking a first pass at it?
How difficult would it be for an Erlang newbie to take a shot at it
for his own use?
Re: Creating attachments from HTML's INPUT type="file"
Posted by Jan Lehnardt <ja...@apache.org>.
Futon can upload files to CouchDB using a HTML form. Check its source
on how it does it :)
Cheers
Jan
--
On 18 Apr 2009, at 21:38, Curt Arnold wrote:
> I've been developing a custom browser-hosted application using
> CouchDB for a client and have so far been able to avoid introducing
> an intermediary between the browser and CouchDB (aka a CouchApp).
> However, I have run into a stumbling block trying to support
> uploading attachments to items from the browser.
>
> The only mechanism that seems to provide access to the local
> filesystem is HTML's file-select control and does not appear to
> provide a mechanism to control the request sent to the action URL so
> that it could conform to the CouchDB API.
>
> Would it be practical or desirable to introduce an endpoint that
> could directly process the request that would be sent from such a
> form?
>
> Would anyone be interested in taking a first pass at it?
>
> How difficult would it be for an Erlang newbie to take a shot at it
> for his own use?
>
Re: Creating attachments from HTML's INPUT type="file"
Posted by Chris Anderson <jc...@apache.org>.
On Thu, Apr 30, 2009 at 9:21 PM, Curt Arnold <ca...@apache.org> wrote:
>
> On Apr 21, 2009, at 10:52 AM, Chris Anderson wrote:
> ...
>>>
>>> This seems like an awful lot of special case weirdness, and there are
>>> probably more edge cases than you've identified so far. And this only
>>> addresses attachments -- there were some interesting ideas posed on the
>>> list
>>> some time back about how to do unobtrusive form handling, but there were
>>> still were some oddities, particularly around strings vs. numbers and
>>> deeply-nested objects.
>>>
>>> I know this is sacrilege, but wouldn't this be best handled by action
>>> servers?
>>
>> This is the way to go. Not action servers exactly, but an update
>> handler, for JavaScript which can translate a Req to JSON for PUT.
>> Patches welcome or I'll get to it eventually.
>>
>>> Of course, you could always bury a form handler in a wsgi or rack
>>> app or whatever (even a jack app in js) as an _external, but obviously
>>> that's not very portable. Baking action servers into couch is the only
>>> sane
>>> way to solve this problem for something like couchapps (a jsonp service
>>> is
>>> out of the question for write ops). And it shouldn't be any trouble with
>>> sharding.
>>>
>>> So maybe action server is the wrong word, and I know it could totally get
>>> abused (hell, I *know *I would abuse it), but wouldn't it be great if
>>> form
>>> handling could be embedded in design docs with all the other app logic?
>>>
>>
>>
>
> Is there an open bug report for "action server" or "update handler" or any
> previous discussions. I searched JIRA and didn't see it, but the terms were
> generic enough that they matched many entries.
>
> I was trying to avoid specifying any implementation details, just the
> requirements to fill a big functionality hole for pure CouchApps that could
> not be handled browser side. Would be great if it could be handled by
> something generic.
>
> Eventually I will need to address this either by trying my first attempt to
> hack Erlang or by adding an intermediary layer and abandon being a CouchApp.
> Would appreciate any advice if I need roll my own solution. Not urgent at
> the moment, but eventually my client will want to see it and I'll be forced
> to find a solution.
>
This is definitely on the roadmap for CouchDB. I'd like to see it in
0.10.0, but I'm hard pressed to find time for writing new features.
There has been other discussion on the dev@couchdb.apache.org list
about the feature. Grep for _update to find the threads.
Creating this feature would mostly involve reusing code that has
already been written for _show, _list, and _external.
--
Chris Anderson
http://jchrisa.net
http://couch.io
Re: Creating attachments from HTML's INPUT type="file"
Posted by Curt Arnold <ca...@apache.org>.
On Apr 21, 2009, at 10:52 AM, Chris Anderson wrote:
...
>>
>> This seems like an awful lot of special case weirdness, and there are
>> probably more edge cases than you've identified so far. And this only
>> addresses attachments -- there were some interesting ideas posed on
>> the list
>> some time back about how to do unobtrusive form handling, but there
>> were
>> still were some oddities, particularly around strings vs. numbers and
>> deeply-nested objects.
>>
>> I know this is sacrilege, but wouldn't this be best handled by action
>> servers?
>
> This is the way to go. Not action servers exactly, but an update
> handler, for JavaScript which can translate a Req to JSON for PUT.
> Patches welcome or I'll get to it eventually.
>
>> Of course, you could always bury a form handler in a wsgi or rack
>> app or whatever (even a jack app in js) as an _external, but
>> obviously
>> that's not very portable. Baking action servers into couch is the
>> only sane
>> way to solve this problem for something like couchapps (a jsonp
>> service is
>> out of the question for write ops). And it shouldn't be any trouble
>> with
>> sharding.
>>
>> So maybe action server is the wrong word, and I know it could
>> totally get
>> abused (hell, I *know *I would abuse it), but wouldn't it be great
>> if form
>> handling could be embedded in design docs with all the other app
>> logic?
>>
>
>
Is there an open bug report for "action server" or "update handler" or
any previous discussions. I searched JIRA and didn't see it, but the
terms were generic enough that they matched many entries.
I was trying to avoid specifying any implementation details, just the
requirements to fill a big functionality hole for pure CouchApps that
could not be handled browser side. Would be great if it could be
handled by something generic.
Eventually I will need to address this either by trying my first
attempt to hack Erlang or by adding an intermediary layer and abandon
being a CouchApp. Would appreciate any advice if I need roll my own
solution. Not urgent at the moment, but eventually my client will
want to see it and I'll be forced to find a solution.
Re: Creating attachments from HTML's INPUT type="file"
Posted by Chris Anderson <jc...@apache.org>.
On Tue, Apr 21, 2009 at 11:44 AM, Dean Landolt <de...@deanlandolt.com> wrote:
> On Mon, Apr 20, 2009 at 11:54 PM, Curt Arnold <ca...@apache.org> wrote:
>
>>
>> On Apr 19, 2009, at 7:01 AM, Jan Lehnardt wrote:
>>
>>
>>> On 19 Apr 2009, at 05:16, Oliver Boermans wrote:
>>>
>>> ...
>>
>>>
>>>> Excellent summary, thanks! We're open to enable the API handlers for
>>> pure HTML forms with more features, if you can line out what you would
>>> want to have.
>>>
>>> Moving to dev@
>>>
>>> Cheers
>>> Jan
>>> --
>>>
>>>
>>
>> I haven't had the time I would like to research this adequately. I thought
>> it would good to keep the discussion going, so please forgive any obvious
>> blunders.
>>
>> Feature: End point for HTML form submissions for file uploading
>> Use case: Uploading files from browser when their is not intermediate
>> layer.
>>
>> Info that must be encoded somewhere:
>>
>> database URL
>> docID
>> rev (for modifications)
>> content type of attachment
>> content of attachment
>> attachment name
>>
>>
>> Possible locations:
>>
>> database URL: That has to go in the action URL for the form
>> Content of attachment: In body of POST request per HTML spec
>> docID: Could be encoded in either the action URL, content of a field
>> (possibly hidden), or field name.
>> rev: Same options
>> Content type: The POST request will have a content-type from the browser in
>> the header for the file content, however the form author may want to allow
>> the user to explicitly specify a content type.
>> Attachment name: Name of input type="file" field, names of files selected,
>> content of hidden field
>>
>> Considerations:
>>
>> Should we provide a mechanism to change the body of the document
>> simultaneously. For example, if there is a field "body", it would be
>> interpreted as JSON and replace the current document contents.
>>
>> A file input control can select and upload multiple files. If attachment
>> name is based on the field name, likely would need to add an index to
>> support the case when there are multiple files selected.
>>
>> The local file names may be missing from the post body, might not be unique
>> and might be approximations. I'd avoid basing the attachment name off of
>> them, thought it would be good if they could be preserved. Any chance that
>> they could be added to the _attachments metadata?
>>
>>
>> My current though is something like:
>>
>> Action URL: http//server:5984/database/_form?multi=true|false
>>
>> Special field names for docID, rev and body
>>
>> If multi=true, multiple selections of files would be supported and
>> attachments would be named from the field name + [index]
>> If multi=false, if multiple files were selected, the request would be
>> rejected. Otherwise attachment would be named after the input field
>> For every file input control, if there is another field with that name +
>> "content-type", the value of that field would be used for the content type.
>> Otherwise, the content type provided by the browser would be used.
>>
>>
>> Ideally, the local names of the uploaded files would be preserved in the
>> metadata for the attachment.
>>
>
> This seems like an awful lot of special case weirdness, and there are
> probably more edge cases than you've identified so far. And this only
> addresses attachments -- there were some interesting ideas posed on the list
> some time back about how to do unobtrusive form handling, but there were
> still were some oddities, particularly around strings vs. numbers and
> deeply-nested objects.
>
> I know this is sacrilege, but wouldn't this be best handled by action
> servers?
This is the way to go. Not action servers exactly, but an update
handler, for JavaScript which can translate a Req to JSON for PUT.
Patches welcome or I'll get to it eventually.
>Of course, you could always bury a form handler in a wsgi or rack
> app or whatever (even a jack app in js) as an _external, but obviously
> that's not very portable. Baking action servers into couch is the only sane
> way to solve this problem for something like couchapps (a jsonp service is
> out of the question for write ops). And it shouldn't be any trouble with
> sharding.
>
> So maybe action server is the wrong word, and I know it could totally get
> abused (hell, I *know *I would abuse it), but wouldn't it be great if form
> handling could be embedded in design docs with all the other app logic?
>
--
Chris Anderson
http://jchrisa.net
http://couch.io
Re: Creating attachments from HTML's INPUT type="file"
Posted by Dean Landolt <de...@deanlandolt.com>.
On Mon, Apr 20, 2009 at 11:54 PM, Curt Arnold <ca...@apache.org> wrote:
>
> On Apr 19, 2009, at 7:01 AM, Jan Lehnardt wrote:
>
>
>> On 19 Apr 2009, at 05:16, Oliver Boermans wrote:
>>
>> ...
>
>>
>>> Excellent summary, thanks! We're open to enable the API handlers for
>> pure HTML forms with more features, if you can line out what you would
>> want to have.
>>
>> Moving to dev@
>>
>> Cheers
>> Jan
>> --
>>
>>
>
> I haven't had the time I would like to research this adequately. I thought
> it would good to keep the discussion going, so please forgive any obvious
> blunders.
>
> Feature: End point for HTML form submissions for file uploading
> Use case: Uploading files from browser when their is not intermediate
> layer.
>
> Info that must be encoded somewhere:
>
> database URL
> docID
> rev (for modifications)
> content type of attachment
> content of attachment
> attachment name
>
>
> Possible locations:
>
> database URL: That has to go in the action URL for the form
> Content of attachment: In body of POST request per HTML spec
> docID: Could be encoded in either the action URL, content of a field
> (possibly hidden), or field name.
> rev: Same options
> Content type: The POST request will have a content-type from the browser in
> the header for the file content, however the form author may want to allow
> the user to explicitly specify a content type.
> Attachment name: Name of input type="file" field, names of files selected,
> content of hidden field
>
> Considerations:
>
> Should we provide a mechanism to change the body of the document
> simultaneously. For example, if there is a field "body", it would be
> interpreted as JSON and replace the current document contents.
>
> A file input control can select and upload multiple files. If attachment
> name is based on the field name, likely would need to add an index to
> support the case when there are multiple files selected.
>
> The local file names may be missing from the post body, might not be unique
> and might be approximations. I'd avoid basing the attachment name off of
> them, thought it would be good if they could be preserved. Any chance that
> they could be added to the _attachments metadata?
>
>
> My current though is something like:
>
> Action URL: http//server:5984/database/_form?multi=true|false
>
> Special field names for docID, rev and body
>
> If multi=true, multiple selections of files would be supported and
> attachments would be named from the field name + [index]
> If multi=false, if multiple files were selected, the request would be
> rejected. Otherwise attachment would be named after the input field
> For every file input control, if there is another field with that name +
> "content-type", the value of that field would be used for the content type.
> Otherwise, the content type provided by the browser would be used.
>
>
> Ideally, the local names of the uploaded files would be preserved in the
> metadata for the attachment.
>
This seems like an awful lot of special case weirdness, and there are
probably more edge cases than you've identified so far. And this only
addresses attachments -- there were some interesting ideas posed on the list
some time back about how to do unobtrusive form handling, but there were
still were some oddities, particularly around strings vs. numbers and
deeply-nested objects.
I know this is sacrilege, but wouldn't this be best handled by action
servers? Of course, you could always bury a form handler in a wsgi or rack
app or whatever (even a jack app in js) as an _external, but obviously
that's not very portable. Baking action servers into couch is the only sane
way to solve this problem for something like couchapps (a jsonp service is
out of the question for write ops). And it shouldn't be any trouble with
sharding.
So maybe action server is the wrong word, and I know it could totally get
abused (hell, I *know *I would abuse it), but wouldn't it be great if form
handling could be embedded in design docs with all the other app logic?
Re: Creating attachments from HTML's INPUT type="file"
Posted by Curt Arnold <ca...@apache.org>.
On Apr 19, 2009, at 7:01 AM, Jan Lehnardt wrote:
>
> On 19 Apr 2009, at 05:16, Oliver Boermans wrote:
>
...
>>
> Excellent summary, thanks! We're open to enable the API handlers for
> pure HTML forms with more features, if you can line out what you would
> want to have.
>
> Moving to dev@
>
> Cheers
> Jan
> --
>
I haven't had the time I would like to research this adequately. I
thought it would good to keep the discussion going, so please forgive
any obvious blunders.
Feature: End point for HTML form submissions for file uploading
Use case: Uploading files from browser when their is not intermediate
layer.
Info that must be encoded somewhere:
database URL
docID
rev (for modifications)
content type of attachment
content of attachment
attachment name
Possible locations:
database URL: That has to go in the action URL for the form
Content of attachment: In body of POST request per HTML spec
docID: Could be encoded in either the action URL, content of a field
(possibly hidden), or field name.
rev: Same options
Content type: The POST request will have a content-type from the
browser in the header for the file content, however the form author
may want to allow the user to explicitly specify a content type.
Attachment name: Name of input type="file" field, names of files
selected, content of hidden field
Considerations:
Should we provide a mechanism to change the body of the document
simultaneously. For example, if there is a field "body", it would be
interpreted as JSON and replace the current document contents.
A file input control can select and upload multiple files. If
attachment name is based on the field name, likely would need to add
an index to support the case when there are multiple files selected.
The local file names may be missing from the post body, might not be
unique and might be approximations. I'd avoid basing the attachment
name off of them, thought it would be good if they could be
preserved. Any chance that they could be added to the _attachments
metadata?
My current though is something like:
Action URL: http//server:5984/database/_form?multi=true|false
Special field names for docID, rev and body
If multi=true, multiple selections of files would be supported and
attachments would be named from the field name + [index]
If multi=false, if multiple files were selected, the request would be
rejected. Otherwise attachment would be named after the input field
For every file input control, if there is another field with that name
+ "content-type", the value of that field would be used for the
content type. Otherwise, the content type provided by the browser
would be used.
Ideally, the local names of the uploaded files would be preserved in
the metadata for the attachment.
Re: Creating attachments from HTML's INPUT type="file"
Posted by Jan Lehnardt <ja...@apache.org>.
On 19 Apr 2009, at 05:16, Oliver Boermans wrote:
> 2009/4/19 Curt Arnold <ca...@apache.org>:
>> The only mechanism that seems to provide access to the local
>> filesystem is
>> HTML's file-select control and does not appear to provide a
>> mechanism to
>> control the request sent to the action URL so that it could conform
>> to the
>> CouchDB API.
>
> Hey Curt,
> Have been tackling this problem myself recently. Here are some notes
> of what I discovered. If I am wrong or others have better techniques
> hopefully they will share them here. Regardless, I hope this helps you
> get to the bottom of it more quickly than I did!
>
> To upload a file from a HTML form to CouchDB (without middleware and
> while preserving it’s mime type ) use a regular form POST to an
> _existing_ document.
>
> Set the name of the file input as _attachments. CouchDB also expects
> to receive a matching revision value for modifications to existing
> documents. A hidden field named _rev in the same form will do that
> providing it has the correct value.
>
> If the uploaded file name matches an existing attachment of the same
> document, the existing file will be replaced. Otherwise the new file
> will be appended to that document’s array of attachments.
>
> To do this with the guise of an ajax operation (file data cannot be
> uploaded with ajax as far as I can tell) I made a small modification
> to the jquery.form plugin <http://malsup.com/jquery/form/>:
>
> Replacing:
> var ta = doc.getElementsByTagName('textarea')[0];
> xhr.responseText = ta ? ta.value :
> xhr.responseText;
> With:
> var ta = doc.getElementsByTagName('pre')[0];
> xhr.responseText = $(ta).text();
>
> The plugin loads the server response into a generated iframe while
> providing access to the server response. Trouble is it expects the
> server to send the json response wrapped in a <textarea> rather than a
> <pre> as CouchDB does.
>
> With the above modification the server response can be handled to grab
> the new rev and to check success. The form plugin will always report
> success when sending file data - calling the success function,
> fortunately it is simple to check the server response for error or ok
> within that function as respond accordingly.
>
> If there is no existing document to attach the file to, one can be
> created using a AJAX PUT first. Grabbing the response for this will
> provide the _rev necessary to further modify it by attachiing your
> file.
>
> The one gotcha I am yet to satisfy is modifying other values of these
> documents with attachments (with a HTML form submission) trashes the
> file data. I guess because POSTing new data without the attachment
> stub means a new document without attachments! My workaround has been
> to make attachment specific documents and save related data in a
> separate documents. Clunky but functional.
Excellent summary, thanks! We're open to enable the API handlers for
pure HTML forms with more features, if you can line out what you would
want to have.
Moving to dev@
Cheers
Jan
--
Re: Creating attachments from HTML's INPUT type="file"
Posted by Jan Lehnardt <ja...@apache.org>.
On 19 Apr 2009, at 05:16, Oliver Boermans wrote:
> 2009/4/19 Curt Arnold <ca...@apache.org>:
>> The only mechanism that seems to provide access to the local
>> filesystem is
>> HTML's file-select control and does not appear to provide a
>> mechanism to
>> control the request sent to the action URL so that it could conform
>> to the
>> CouchDB API.
>
> Hey Curt,
> Have been tackling this problem myself recently. Here are some notes
> of what I discovered. If I am wrong or others have better techniques
> hopefully they will share them here. Regardless, I hope this helps you
> get to the bottom of it more quickly than I did!
>
> To upload a file from a HTML form to CouchDB (without middleware and
> while preserving it’s mime type ) use a regular form POST to an
> _existing_ document.
>
> Set the name of the file input as _attachments. CouchDB also expects
> to receive a matching revision value for modifications to existing
> documents. A hidden field named _rev in the same form will do that
> providing it has the correct value.
>
> If the uploaded file name matches an existing attachment of the same
> document, the existing file will be replaced. Otherwise the new file
> will be appended to that document’s array of attachments.
>
> To do this with the guise of an ajax operation (file data cannot be
> uploaded with ajax as far as I can tell) I made a small modification
> to the jquery.form plugin <http://malsup.com/jquery/form/>:
>
> Replacing:
> var ta = doc.getElementsByTagName('textarea')[0];
> xhr.responseText = ta ? ta.value :
> xhr.responseText;
> With:
> var ta = doc.getElementsByTagName('pre')[0];
> xhr.responseText = $(ta).text();
>
> The plugin loads the server response into a generated iframe while
> providing access to the server response. Trouble is it expects the
> server to send the json response wrapped in a <textarea> rather than a
> <pre> as CouchDB does.
>
> With the above modification the server response can be handled to grab
> the new rev and to check success. The form plugin will always report
> success when sending file data - calling the success function,
> fortunately it is simple to check the server response for error or ok
> within that function as respond accordingly.
>
> If there is no existing document to attach the file to, one can be
> created using a AJAX PUT first. Grabbing the response for this will
> provide the _rev necessary to further modify it by attachiing your
> file.
>
> The one gotcha I am yet to satisfy is modifying other values of these
> documents with attachments (with a HTML form submission) trashes the
> file data. I guess because POSTing new data without the attachment
> stub means a new document without attachments! My workaround has been
> to make attachment specific documents and save related data in a
> separate documents. Clunky but functional.
Excellent summary, thanks! We're open to enable the API handlers for
pure HTML forms with more features, if you can line out what you would
want to have.
Moving to dev@
Cheers
Jan
--
Re: Creating attachments from HTML's INPUT type="file"
Posted by Oliver Boermans <bo...@gmail.com>.
2009/4/19 Curt Arnold <ca...@apache.org>:
> The only mechanism that seems to provide access to the local filesystem is
> HTML's file-select control and does not appear to provide a mechanism to
> control the request sent to the action URL so that it could conform to the
> CouchDB API.
Hey Curt,
Have been tackling this problem myself recently. Here are some notes
of what I discovered. If I am wrong or others have better techniques
hopefully they will share them here. Regardless, I hope this helps you
get to the bottom of it more quickly than I did!
To upload a file from a HTML form to CouchDB (without middleware and
while preserving it’s mime type ) use a regular form POST to an
_existing_ document.
Set the name of the file input as _attachments. CouchDB also expects
to receive a matching revision value for modifications to existing
documents. A hidden field named _rev in the same form will do that
providing it has the correct value.
If the uploaded file name matches an existing attachment of the same
document, the existing file will be replaced. Otherwise the new file
will be appended to that document’s array of attachments.
To do this with the guise of an ajax operation (file data cannot be
uploaded with ajax as far as I can tell) I made a small modification
to the jquery.form plugin <http://malsup.com/jquery/form/>:
Replacing:
var ta = doc.getElementsByTagName('textarea')[0];
xhr.responseText = ta ? ta.value : xhr.responseText;
With:
var ta = doc.getElementsByTagName('pre')[0];
xhr.responseText = $(ta).text();
The plugin loads the server response into a generated iframe while
providing access to the server response. Trouble is it expects the
server to send the json response wrapped in a <textarea> rather than a
<pre> as CouchDB does.
With the above modification the server response can be handled to grab
the new rev and to check success. The form plugin will always report
success when sending file data - calling the success function,
fortunately it is simple to check the server response for error or ok
within that function as respond accordingly.
If there is no existing document to attach the file to, one can be
created using a AJAX PUT first. Grabbing the response for this will
provide the _rev necessary to further modify it by attachiing your
file.
The one gotcha I am yet to satisfy is modifying other values of these
documents with attachments (with a HTML form submission) trashes the
file data. I guess because POSTing new data without the attachment
stub means a new document without attachments! My workaround has been
to make attachment specific documents and save related data in a
separate documents. Clunky but functional.
Cheers
Ollie