1

I am currently trying to upload file with javascript to Google Drive. Everything works fine, but I am not sure what I have to add to give name to my uploaded file.

function uploadFile(){
    var form = new FormData();
    var xhttp = new XMLHttpRequest();
    xhttp.responseType = 'blob';
    var fileID = document.getElementById('fileName').value;
    var apiKeyId = "{MY_API_KEY}";

    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            console.log('Uploaded');
        }
    };
    xhttp.open("POST", "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", true);
    xhttp.setRequestHeader("Authorization", "Bearer "+userToken); 

    var sendFiles = document.getElementById("sendFile");
    if ('files' in sendFiles) {
        if (sendFiles.files.length == 0) {
        } else {
            for (var i = 0; i < sendFiles.files.length; i++) {
                var file = sendFiles.files[i];
                xhttp.send(file);
            }
        }
    } 
}
VSmoL
  • 47
  • 2
  • 6

1 Answers1

0

from my understanding you want to add a file name, so here you give a date and time as a name of the file with separated by any symbol so you can later what is the actual file name of the file

Nik Varma
  • 158
  • 9
  • That's right, I want to add a file name. The question is what as a code I need to add to do that? I want to add final file name to file, not to go and change it later in Google Drive. – VSmoL Apr 25 '16 at 06:26
  • take the file extension from your code which you are using to place a file in google drive and take the datetime, finally you have to do that concat the both file name and file extension with the actual file name : example file.png is your actual file name , after uploading datetime-file.png(for unique file name) 20160425045000-file.png – Nik Varma Apr 25 '16 at 11:16
  • http://stackoverflow.com/questions/857618/javascript-how-to-extract-filename-from-a-file-input-control – Nik Varma Apr 25 '16 at 11:18