0

在这里,我有一个表单,其中我有一个输入类型文件,当单击上传按钮时上传我的文件我需要将 multipart/form-data 发布到我将文件上传到 Minio Server 的 web api。我已经粘贴了 javascript和我在下面使用的 web api。

当我在收到 500(内部服务器错误)后按下上传按钮时。请帮我提出建议。

包含输入类型文件和上传按钮的 HTML

$("#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;
            }
    }
4

2 回答 2

0

我认为您无需提及 localhost 只需文件的路径即可。或将其替换为本地主机的 IP。

于 2017-08-10T06:42:17.997 回答
0

对不起,我没有记错我在 javascript 中附加的名称没有保存为我在 web api 中给出的名称。

我变了,

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

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

它奏效了。

于 2017-08-10T07:33:11.570 回答