You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by Carl Douglas <ca...@gmail.com> on 2009/11/03 05:44:17 UTC

Changing an attachment content-type through Futons source editor.

Hi,

Not sure if this is a bug, a feature or documentation request.

I am using XMLHttpRequest to PUT an attachment to an existing
document. This works, see my client side JavaScript below.

However I noticed sometimes someone uploads an attachment but Firefox
got the content-type wrong (done on the client side with my SUPER
HACK).  To correct this, I use Futon to edit the document JSON source
and change the content_type and save the document. But my change to
the content_type field does not get saved.

I guess I can take a look at the Futon source, but thought I'd ask
here first for suggestions.

Carl

<script type="text/javascript">
 $(function() {
   $('#upload-form').submit(function(){
     // Have to ask for the doc JSON in order to get the rev
     // since we can't PUT or DELETE attachments without the current rev.
     // Not having the current rev, results in 409 conflict.
     $.getJSON("<%= docURL %>", function(doc){
       // Firefox 3 only...
       var file = $('#upload-form #file')[0].files.item(0);
       var xhr = new XMLHttpRequest();

       xhr.open("PUT", "<%= docURL %>/" + file.fileName + "?rev=" +
doc._rev, true);
       xhr.onreadystatechange = function() {
         if (xhr.readyState == 4) {
           if (xhr.status == 201) {
             // OK!
            } else {

            }
         }
       };
       xhr.setRequestHeader('Content-Length', file.fileSize);
       // Mime-type SUPER HACK
       xhr.setRequestHeader("Content-Type",
file.getAsDataURL().match(/data:([^;]*);/)[1]);
       xhr.sendAsBinary(file.getAsBinary());
     });
     return false;
   });
 });
</script>

Re: Changing an attachment content-type through Futons source editor.

Posted by Carl Douglas <ca...@gmail.com>.
Hi Paul,

On Tue, Nov 3, 2009 at 4:02 PM, Paul Davis <pa...@gmail.com> wrote:
> Carl,
>
>> However I noticed sometimes someone uploads an attachment but Firefox
>> got the content-type wrong (done on the client side with my SUPER
>> HACK).  To correct this, I use Futon to edit the document JSON source
>> and change the content_type and save the document. But my change to
>> the content_type field does not get saved.
>
> I'm actually not entirely sure what would happen in this scenario. I
> don't think we test it anywhere. Since futon grabs the _attachments
> data as a stub, saving it back to the DB with the non-inline style
> should just ignore any changes you make. And now that you mention it,
> I don't think there's actually a way to change the content-type
> without re-uploading the binary data. That definitely seems like
> overlooked functionality, but not quite a bug.
>
>>       // Mime-type SUPER HACK
>>       xhr.setRequestHeader("Content-Type",
>> file.getAsDataURL().match(/data:([^;]*);/)[1]);
>>       xhr.sendAsBinary(file.getAsBinary());
>
> I have never ever seen JavaScript that does such things. Is this
> actually possible in a cross browser fashion or some FF extension?
>
> Paul Davis
>

Yes that JavaScript relies on Mozilla Firefox 3 specific interfaces:

https://developer.mozilla.org/en/NsIDOMFile
https://developer.mozilla.org/en/nsIDOMFileList

Thanks

Re: Changing an attachment content-type through Futons source editor.

Posted by Paul Davis <pa...@gmail.com>.
Carl,

> However I noticed sometimes someone uploads an attachment but Firefox
> got the content-type wrong (done on the client side with my SUPER
> HACK).  To correct this, I use Futon to edit the document JSON source
> and change the content_type and save the document. But my change to
> the content_type field does not get saved.

I'm actually not entirely sure what would happen in this scenario. I
don't think we test it anywhere. Since futon grabs the _attachments
data as a stub, saving it back to the DB with the non-inline style
should just ignore any changes you make. And now that you mention it,
I don't think there's actually a way to change the content-type
without re-uploading the binary data. That definitely seems like
overlooked functionality, but not quite a bug.

>       // Mime-type SUPER HACK
>       xhr.setRequestHeader("Content-Type",
> file.getAsDataURL().match(/data:([^;]*);/)[1]);
>       xhr.sendAsBinary(file.getAsBinary());

I have never ever seen JavaScript that does such things. Is this
actually possible in a cross browser fashion or some FF extension?

Paul Davis