Updating Blob Name

Viewed 127

I'm having an issue Updating a blobs name - I have done this no problem before but in this case I'm storing the blob in IndexedDB and based on certain conditions (save/saveas) it gets a name from Google Drive or you can add a new name.

Dom JS File

      //Convert Text to Blob
  let file = text;

  fileName = "NewFileName";

  let metadata = {
    name: fileName, // Filename
    mimeType: "application/pdf", // mimeType at Google Drive
  };

  let form = new FormData();
  form.append(
    "metadata",
    new Blob([JSON.stringify(metadata)], { type: "application/json" })
  );
  form.append("file", file);

  let textBlob = new Blob([file], {
    'type': 'application/pdf',
  });

Then My ServiceWorker Receives it and renames it then uploads it to google Drive

    let blobPDF = request.result.text;

    let blob = new Blob([blobPDF], {
    type: "application/pdf"
    });
    let newBlob = new FormData();

    console.log("Blob Name : " + saveas)
    //Set New Name
    newBlob.append("blob", blob, saveas);

    fetch(
      "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id",
      {
        method: "POST",
        headers: new Headers({ Authorization: "Bearer " + accessToken }),
        body: blob  // Also tried newBlob var -> Got not a blob error
      }
    )

if I use the var newBlob it says it's not a blob and errors - then changed to "file" it still doesn't set name

1 Answers

One obvious mistake is that you're sending the blob itself as the body of your fetch request, not the newBlob form data object with the filename.

fetch("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id", {
  method: "POST",
  headers: new Headers({ Authorization: "Bearer " + accessToken }),
  body: newBlob
//        ^^^^
})

If that doesn't help, I'd try creating a File instead of a Blob:

let blob = new File([blobPDF], saveas, {
  type: "application/pdf"
});
Related