0

My json object that I plan to post is something like below

export class MyPayload{
    Customer: string;
    SalesTotal: number;
    ...deleted whole bunch of other stuff and finally
    salesForcastCsvfile: blob; // ?? I want to insert file serialized here to send to asp.net core api via a post
  }

I know how to upload just the file direct as explained here But iam struggling to get the file into memory and serialize and insert in my json payload. Please advise how to do. Could not find any online tutorial for this.

Jonathan Anctil
  • 1,025
  • 3
  • 20
  • 44
Gullu
  • 3,477
  • 7
  • 43
  • 70
  • Have you try converting your file in a base64 format? https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding – Jonathan Anctil Sep 28 '18 at 18:50
  • @JonathanAnctil, i am loading the file as explained here https://stackoverflow.com/questions/51026420/filereader-readastext-async-issues . It's just this line content = e.target.result; Now how do I stuff this content into my json payload , how to convert to base64 , what is the datatype in my json for content ?? . – Gullu Sep 28 '18 at 18:56
  • If the file you upload is a JSON file, use JSON.stringify and store it as a string type value. – Jonathan Anctil Sep 28 '18 at 18:59
  • @JonathanAnctil, it's a csv file. I could just convert the file to string and send as you mentioned but requirement is to store the file as a file in sql server. So i was guessing I need to serialize and send as a blob or something..like we do for word docs and stuff i guess. – Gullu Sep 28 '18 at 19:03
  • 1
    Ok, I see. You store it as varbinary type... I think the best way is to convert the file into base64 format (https://stackoverflow.com/questions/36280818/how-to-convert-file-to-base64-in-javascript), and on server side, converting that base64 value into binary (https://stackoverflow.com/questions/27678013/storing-c-sharp-converted-base64-as-sql-server-varbinarymax). – Jonathan Anctil Sep 28 '18 at 19:10
  • @JonathanAnctil, thanks for your pointers. Will try it out next week and get back if any issues. – Gullu Sep 28 '18 at 19:58
  • 1
    @JonathanAnctil, all good works as expected. Thanks – Gullu Oct 01 '18 at 20:43

0 Answers0