0

I'm trying to make a "simple" file upload.

Front end is javascript and backend is .net core.

I had this working with FormData / multipart , FromForm and iFormFile but I've been instructed not to use this approach (it blows swagger for some reason).

I've been trying to use FileReader and readAsDataURL but I keep getting a 500 code on the .net side.

How should I code the controller to be able to read the DataURL format?

Nelson André
  • 35
  • 1
  • 9

2 Answers2

0

If you dont want to use FormData you have to set the Headers appropriately and send Binary Data. Dont encode or process anything.

You can just add you file as the content of a simple request without FormData. The .Net side shouldnt change at all.

see here: https://stackoverflow.com/a/28212708/5238618

0

I've been trying to use FileReader and readAsDataURL but I keep getting a 500 code on the .net side.

How should I code the controller to be able to read the DataURL format?

If you'd like to use FileReader.readAsDataURL() to get a base64 encoded string of the selected image, and post it to a specific controller action, you can make that controller action accept a string type parameter, like below.

[HttpPost]
public IActionResult UploadFile([FromBody]string base64Encoded_Img)
{
    //...

Make request from JS client

data_url = reader.result;
$.ajax({
    type: 'Post',
    contentType: 'application/json; charset=utf-8',
    url: "https://xxx/xxxx/uploadfile",
    data: JSON.stringify(data_url),
    success: function (json) {
        console.log(json);
    },
    //...
});

Test Result

enter image description here

Fei Han
  • 26,415
  • 1
  • 30
  • 41