K
K
KirylLapouski2018-05-26 22:36:31
JavaScript
KirylLapouski, 2018-05-26 22:36:31

How to upload a local file from a client to Yandex disk?

For some reason this code doesn't work. It is clear that the problem is that I incorrectly attach the file to the put request. But what is right?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <form name="upload" action="POST">
        <input type="file" name="fileInput" id="">
        <input type="submit">
    </form>

    <div id="log">Прогресс загрузки</div>

    <script>
            function log(html) {
      document.getElementById('log').innerHTML = html;
    }

        document.forms.upload.onsubmit = function (e) {
            e.preventDefault()
            var input = this.elements.fileInput
            var file = input.files[0]
            console.log(file)
            var xhr = new XMLHttpRequest();

            xhr.upload.onprogress = function (event) {
                log(event.loaded + ' / ' + event.total);
            }

            xhr.onload = xhr.onerror = function () {
                if (this.status == 200) {
                    log("success");
                } else {
                    log("error " + this.status);
                }
            };

            xhr.open("PUT", "https://uploader6g.disk.yandex.net:443/upload-target/20180526T222306.946.utd.2ug0h90jo72u3328vvjf4ynzr-k6g.6911714", true);
            xhr.send(file);
        }
        
    </script>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2018-05-27
@402d

https://tech.yandex.ru/disk/api/reference/upload-d...
I hope you get the URL as described above. Valid once every 30 minutes
xhr.open("PUT", url, true);
xhr.setRequestHeader('Content-type','here you must enter the required one');
xhr.send( File_content );
those. you need to somehow read the content

K
KirylLapouski, 2018-05-28
@KirylLapouski

Oleg Muraveyko Yes, that's how I get the url. The question was: how to read the content, and then make a request to send this content to the server?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question