How to read and write into file using JavaScript?

后端 未结 17 2476
孤街浪徒
孤街浪徒 2020-11-22 02:00

Can anybody give some sample code to read and write a file using JavaScript?

17条回答
  •  孤街浪徒
    2020-11-22 02:47

    Here is write solution for chrome v52+ (user still need to select a destination doe...)
    source: StreamSaver.js

    
    
    
    
    const writeStream = streamSaver.createWriteStream('filename.txt')
    const encoder = new TextEncoder
    let data = 'a'.repeat(1024)
    let uint8array = encoder.encode(data + "\n\n")
    
    writeStream.write(uint8array) // must be uInt8array
    writeStream.close()
    

    Best suited for writing large data generated on client side.
    Otherwise I suggest using FileSaver.js to save Blob/Files

提交回复
热议问题