在dotnet核心中上传文件

时间:2018-02-25 17:29:33

标签: c# .net-core

我试图按照这里的dotnet核心示例:

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

在控制器端进行一些小的调整。以下是我的表单:

<form method="post" enctype="multipart/form-data" asp-controller="Test" asp-action="DoUpload">
<div class="form-group">
    <div class="col-md-10">
        <p>Upload one or more files using this form:</p>
        <input type="file" name="files" multiple />
    </div>
</div>
<div class="form-group">
    <div class="col-md-10">
        <input type="submit" value="Upload" />
    </div>
</div>

需要注意的事项:asp-controller和asp-action只是更改为将表单指向不同的控制器。此外,唯一输入的名称是&#34;文件&#34;。 enctype设置为&#34; multipart / form-data&#34;。

所以相应的控制器代码就是以下内容:

public JsonResult DoUpload (List<IFormFile> files) {

        return Json(files.Count);

 }

所以期望它只是返回提交的文件数 - 这似乎永远不会起作用。我尝试了很多不同的变化,但似乎没有任何效果。我尝试了以下方法:

//public JsonResult DoUpload([FromForm] Object obj, [FromForm] IFormFile file) { <-- Model bound complex types must not be abstract or value types and must have a parameterless constructor.
    //public async Task<IActionResult> Post(List<IFormFile> files) <- files is always empty
    //public JsonResult DoUpload(IFormFile files) { <- Model bound complex types must not be abstract or value types and must have a parameterless constructor.
    //public JsonResult DoUpload(UploadedDocument files) { <- attachment attribute, which is IFormFile is null
    //public JsonResult DoUpload ( [FromBody] UploadedDocument files) { <- 415 Unsupported Media Type
    //public JsonResult DoUpload ( [FromForm] UploadedDocument files) { <- {"attachment":null}
    //public JsonResult DoUpload (UploadedDocument file, IFormFile doc) { <- Model bound complex types must not be abstract or value types and must have a parameterless constructor.
    //public JsonResult DoUpload (List<IFormFile> files ) { <- files is null 
    //public JsonResult DoUpload (Object files) { <- returns {} when (attempting to return Json(files))

谁能看到我做错了什么?如果有一个简单的解决办法,我真的很抱歉,但我似乎无法在任何其他地方找到解决方案或帮助

0 个答案:

没有答案