0

Here i have a form in which i have a input type file to upload my file when the upload button is click i need to post the multipart/form-data to web api where i upload the file to Minio Server.I have pasted the javascript and web api i use below.

When i press upload button after i get 500 (Internal Server Error).Help me with suggestions.

HTML that contains the input type file and upload button

$("#upload").click(function () {
            var file = new FormData($('#uploadform')[0]);
            file.append('tax_file', $('input[type=file]')[0].files[0]);
            $.ajax({
                type: "POST",
                url: 'http://localhost:53094/api/values',
                data: file,
                //use contentType, processData for sure.
                contentType: "multipart/form-data",
                processData: false,
                beforeSend: function () {},
                success: function (msg) {
                    $(".modal .ajax_data").html("<pre>" + msg +
                        "</pre>");
                    $('#close').hide();
                },
                error: function () {
                    $(".modal .ajax_data").html(
                        "<pre>Sorry! Couldn't process your request.</pre>"
                    );  
                    $('#done').hide();
                }
            });
        });

[HttpPost]
    public string Post(IFormFile file)
    {
        try
            {
                var stream = file.OpenReadStream();
                var name = file.FileName;
                minio.PutObjectAsync("student-maarklist", "sample.jpeg", stream, file.Length);
                return "Success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
    }

2 Answers2

0

I think you need not mention localhost just the path to the file will do. or replace it with IP of the localhost.

Harish
  • 1
  • 3
0

Sorry i have dont a mistake the name i appended in javascript is not save as the name i gave in web api.

I changed,

file.append('tax_file', $('input[type=file]')[0].files[0]);

To

file.append('file', $('input[type=file]')[0].files[0]);

and it worked .