0

I am creating this js function:

export function convertArrayObjToFileDownload(data, filename, filetype = 'application/octet-stream') {
    const blob = new Blob(data, { type: filetype });
    downloadBlob(blob, filename);
}

data is a custom object array, like {Client: 'My Bank', Tasks: 15, Charge Amount: '$300.00' }.

I am trying to convert this object array to blob. The array could have hundreds of items.

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
Lizi
  • 93
  • 16
  • What type of data is `data`? Is it an object or a string formatted in JSON, for example? In other words is `data` actually an object with properties like `Client`, `Tasks` etc, or is it text that reads as you wrote? Also, `"Charge Amount"`, while a valid property name, produces a syntax error expressed like you did -- with a space between `Charge` and `Amount` -- you will then have to quote it as `"Charge Amount"`, followed by the `:`. Also, where is the array and its items that you mention can be in the hundreds? I only see a benign object. – Armen Michaeli Jun 30 '22 at 17:56
  • data is actually an object with properties – Lizi Jun 30 '22 at 18:00
  • 1
    If you have an object, you somehow will need to serialise it. JSON is an obvious and easy choice, but there are others - your choice. What do you want? – Bergi Jun 30 '22 at 18:42
  • anything that works. json.stringify ok? – Lizi Jul 01 '22 at 13:32

1 Answers1

1

You're very close! Here's the problem:

The Blob() constructor accepts an Array as the first parameter:
https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob#parameters

And the Array needs to contain a serialized representation of the data you want to store. For a JavaScript object, that's typically JSON. So first convert your data object to a JSON string, and then store that string in the Blob:

const json = JSON.stringify(data);
const blob = new Blob([json], { type: 'text/plain;charset=utf-8' })

Since you'll be storing a string, I'd recommend using the text/plain;charset=utf-8 MIME type or application/json for JSON specifically.

Once you download this Blob, it'll be easy to open in any text editor since it's just plaintext.

  • 2
    Why use `text/plain` instead of [the proper `application/json`](https://stackoverflow.com/q/477816/1048572)? – Bergi Jun 30 '22 at 19:12
  • so this is not either working. 2 problems: 1. I get error after the download: he file format and extension of 'Tasks_For_June_2022 (17).xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway? 2. the data is displayed as json in row 1a – Lizi Jul 01 '22 at 13:22
  • What do you expect to work, exactly, @Lizi? You didn't mention you will be downloading Excel files, did you? JSON-formatted text files and Excel (binary) files are dissimilar enough your Web browser thinks the user is being cheated hence the warning. Do you know what MIME types are? – Armen Michaeli Jul 01 '22 at 13:57
  • no. are you saying it's not possible? – Lizi Jul 01 '22 at 14:04