404 при загрузке больших файлов через API Google Drive - PullRequest
1 голос
/ 18 октября 2019

Я пытаюсь загрузить файлы на Google Drive через их API. Когда размер файла превышает примерно 60 МБ, он, похоже, дает мне 404 или что-то пошло не так.

Я пробовал несколько методов и заметил, что в 2015 году многие люди сталкивались с подобными проблемами, потому что Google не поддерживал возобновляемыйзагрузка из браузера. Я не знаю, изменилось ли это или нет, но, похоже, он отлично работает для файлов размером менее 60 МБ.

//const file = new File(['Hello, world!'], 'hello world.txt', { type: 'text/plain;charset=utf-8' });
    const contentType = file.type || 'application/octet-stream';
    const user = gapi.auth2.getAuthInstance().currentUser.get();
    const oauthToken = user.getAuthResponse().access_token;
    const initResumable = new XMLHttpRequest();
    initResumable.open('POST', 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable', true);
    initResumable.setRequestHeader('Authorization', 'Bearer ' + oauthToken);
    initResumable.setRequestHeader('Content-Type', 'application/json');
    initResumable.setRequestHeader('X-Upload-Content-Length', file.size);
    initResumable.setRequestHeader('X-Upload-Content-Type', contentType);

    initResumable.onreadystatechange = function () {
        if (initResumable.readyState === XMLHttpRequest.DONE && initResumable.status === 200) {
            const locationUrl = initResumable.getResponseHeader('Location');
            const reader = new FileReader();
            reader.onload = (e) => {
                const uploadResumable = new XMLHttpRequest();
                uploadResumable.open('PUT', locationUrl, true);
                uploadResumable.setRequestHeader('Content-Type', contentType);
                uploadResumable.setRequestHeader('X-Upload-Content-Type', contentType);
                uploadResumable.onreadystatechange = function () {
                    if (uploadResumable.readyState === XMLHttpRequest.DONE && uploadResumable.status === 200) {
                        console.log(uploadResumable.response);
                    }
                };
                uploadResumable.send(reader.result);
            };
            reader.readAsArrayBuffer(file);
        }
    };

    // You need to stringify the request body containing any file metadata

    initResumable.send(JSON.stringify({
        'name': file.name,
        'mimeType': contentType,
        'Content-Type': contentType,
        'Content-Length': file.size
    }));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...