Creating a file from an XMLHttpRequest object response

First of all, let me apologize if none of what I'm going to say makes any sense. I feel good here and don’t know what I’m doing. With that said ...

I use the Google Drive file library for files so that users can download their resume from their Google Drive.

function downloadGDriveFile(file) {
    if (file.downloadUrl) {
        var accessToken = gapi.auth.getToken().access_token;
        var xhr = new XMLHttpRequest();
        xhr.open('GET', file.downloadUrl);
        xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
        xhr.onload = function () {
            var content = xhr.response;
            console.log(xhr);

            form_element = document.getElementById("new_guest_application");
            form_data = new FormData(form_element);
            new_file = new File([content], file.title, {type: file.mimeType});
            form_data.append('guest_application[curriculum_vitae]', new_file);
            console.log(new_file);

            var request = new XMLHttpRequest();
            request.open("POST", form_element.action);
            request.send(form_data);

        };
        xhr.onerror = function () {
            alert('Download failure.');
        };
        xhr.send();
    }
    else {
        alert('Unable to download file.');
    }
}

Using the code above, I accept the selected object fileand use it along with the corresponding header marker to download the contentfile. It seems to be working as intended.

My problem arises when I try to turn the specified content into fileto join the form and submit to my server.

, , . , , - file = new File([content], file.title, {type: file.mimeType});, , , .

(, , JavaScript, /)

- ? .

+4
1

xhr.response , xhr.responseType. responseType , .

xhr.responseType

xhr.responseType = "arraybuffer";
var content = xhr.response;
+2

Source: https://habr.com/ru/post/1673002/


All Articles