简体   繁体   中英

Sending a CSV from JavaScript to Firebase Cloud Storage

I'm trying to send a CSV into Firebase Cloud Storage but not from the user manually selecting the file from their file explorer. Rather, on the click of a button, a CSV file will be created in JavaScript and this is what I want to upload to Cloud Storage.

The code snippet shows how I'm creating the file

 function sendToCloudStore() { const rows = [["name1", "city1", "some other info"], ["name2", "city2", "more info"]]; let csvContent = "data:text/csv;charset=utf-8,"; rows.forEach(function(rowArray){ let row = rowArray.join(","); csvContent += row + "\\r\\n"; }); } 
 <input type="button" value="Click Me" onclick="sendToCloudStore()"> 

But I don't know how I get this into Storage. I thought doing this:

storageRef.child('path/to/CSV').put(csvContent)

Would do the trick but it returns the following error

{code_: "storage/invalid-argument", message_: "Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File.", serverResponse_: null, name_: "FirebaseError"}

Can anybody tell me how I can upload this file directly in the same button click action?

Many thanks,

George

You're calling put() , and it's telling you that it's expecting either a File or a Blob object. But you're passing it a string. If you want to upload a string, use the putString() method instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM