Question

We are developing a javascript library for accessing webdav server. And find difficulties in developing upload functionality. We think we should use HTTP PUT for uploading and developed our upload codes using HTTP PUT. It was OK to upload files using http put to a webdav server.

But when we try to upload empty content using HTTP PUT, server replies 411 ( Length required ). We set content-length header but server still replies 411. We don't know why.

  1. Is it right to develop webdav upload functionality using HTTP PUT?
  2. If it is so, how can we develop to create 0 size file using webdav?

The following is our test codes.

<!DOCTYPE html>
<html>
<head>
    <title>Upload Testing</title>
    <script type="text/javascript">

        function ready() {
            alert('ready');
            doPUT('http://hqz.witkitty.com/soliton.co.jp/modc/emptyfile.txt', '', function () {
                alert('success');
            }, function (e) {
                alert('error ' + e.status);
            });
        }

        function doPUT(path, data, callback, errback) {
            alert("going to upload");
            var request = createXMLHttpRequest();
            request.onreadystatechange = function () {
                if (request.readyState == 4) {
                    if (request.status == 201 || request.status == 204) {
                        if (callback) callback(request);
                    } else {
                        if (errback) errback(request);
                    }
                }
            }
            request.open('PUT', path, true);
            request.setRequestHeader('Content-type', 'application/pdf');
            request.setRequestHeader('Content-length', 0);

            request.send(data);
        }

        function createXMLHttpRequest() {
            return window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
        }
    </script>
</head>
<body>
<input type="button" onclick="ready()" value='Test Uploading'/>
</body>
</html>

No correct solution

OTHER TIPS

That sounds like a bug, either in the user agent or in the server.

a) Does this happen with all user agents?

b) Can you capture an HTTP trace and check whether the Content-Length header field is present?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top