Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:02:19+00:00 2026-06-09T12:02:19+00:00

I don’t get attachment upload for the browser to work. Some hints are here

  • 0

I don’t get attachment upload for the browser to work.

Some hints are here, others there. The docs are quite good but I’m unable to translate that to a AJAX upload.

I’m looking for a super simple HTML/JavaScript example (with or w/o jQuery) of how to upload a file from (relatively modern) browser to the db without making use of jquery.couch.app.js wrapper or stuff. The simpler the besser.

Any help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:02:20+00:00Added an answer on June 9, 2026 at 12:02 pm

    Alright, here’s your pure JavaScript file upload implementation.

    The basic algorithm is like this:

    1. Get the file from the file input element
    2. Get the file name and type off the file object
    3. Get the latest document revision of the document you want to attach the file to
    4. Attach the file to document using the fetched revision

    The HTML part basically consists of a simple form with two elements, an input of type file and a button of type submit.

    <form action="/" method="post" name="upload">
      <input type="file" name="file" />
      <button type="submit" name="submit">Upload</button>
    </form>
    

    Now to the JavaScript part.

    window.onload = function() {
        var app = function() {
            var baseUrl = 'http://127.0.0.1:5984/playground/';
            var fileInput = document.forms['upload'].elements['file'];
            document.forms['upload'].onsubmit = function() {
                uploadFile('foo', fileInput.files[0]);
                return false;
            };
    
            var uploadFile = function(docName, file) {
                var name = encodeURIComponent(file.name),
                type = file.type,
                fileReader = new FileReader(),
                getRequest = new XMLHttpRequest(),
                putRequest = new XMLHttpRequest();
    
                getRequest.open('GET',  baseUrl + encodeURIComponent(docName),
                    true);
                getRequest.send();
                getRequest.onreadystatechange = function(response) {
                    if (getRequest.readyState == 4 && getRequest.status == 200) {
                        var doc = JSON.parse(getRequest.responseText);
                        putRequest.open('PUT', baseUrl +
                            encodeURIComponent(docName) + '/' +
                            name + '?rev=' + doc._rev, true);
                        putRequest.setRequestHeader('Content-Type', type);
                        fileReader.readAsArrayBuffer(file);
                        fileReader.onload = function (readerEvent) {
                            putRequest.send(readerEvent.target.result);
                        };
                        putRequest.onreadystatechange = function(response) {
                            if (putRequest.readyState == 4) {
                                console.log(putRequest);
                            }
                        };
                    }
                };
            };
        };
        app();
    };
    

    Basically, I intercept the submit event of the form by binding my own function to the form’s onsubmit event and returning false.

    In that event handler I call my main function with two parameters. The first one being the document name and the second one being the file to upload.

    In my uploadFile() function I set the file name, file type and grab some instances. The first HTTP request is a GET request to obtain the current revision of the document. If that request succeeds I prepare the PUT request (the actual upload request) by setting the previously obtained revision, the proper content type and then I convert the file to an ArrayBuffer. Once that’s done I just send the HTTP request I’ve just prepared and then I relax.

    The standalone attachment upload scheme looks like this:

    PUT host/database/document/filename?revision=latest-revision
    

    Of course using the proper content type in the HTTP request header.

    Note: I’m well aware that I’m not making use of defensive programming here at all, I did that deliberately for brevity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some
Don't think there's much to say, here's my code for (int i = 0;
Don't ask how I got there, but I was playing around with some masking,
don't know what happened in my jcrop selection. I think I pressed some keys
Don't know how to google for such, but is there a way to query
Don't know how to explain it better but i'm trying to get a response
Don't they both have to convert to machine code at some point to execute
I don't know: if this works. if it's a good idea. what it is
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't overthink this - there's a very commonly used term and I ... have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.