incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Lehnardt <...@apache.org>
Subject Re: Creating attachments from HTML's INPUT type="file"
Date Sun, 19 Apr 2009 12:01:53 GMT

On 19 Apr 2009, at 05:16, Oliver Boermans wrote:

> 2009/4/19 Curt Arnold <carnold@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
--


Mime
View raw message