asp.net核心文件上传始终为null

时间:2019-05-15 10:20:16

标签: c# asp.net-core asp.net-core-mvc

我一直在尝试遵循文档中的this示例,以允许将文件上传到我的控制器,它确实达到了我的目的,但是始终以null表示。

我的视图模型

<form method="post" enctype="multipart/form-data" asp-controller="Data" asp-action="ImportAdditionalCodes">
    <div class="form-group">
        <div class="col-md-10">
            <p>Upload import data:</p>
            <input type="file" name="files" >
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-10">
            <input type="submit" value="Upload">
            <button type="button" id="btnCancelUploadData">Cancel</button>
        </div>
    </div>
</form>

我的控制器操作

[HttpPost]
public async Task<IActionResult> ImportAdditionalCodes(IFormFile file)
{
    //file is always null here!!!
    if (file?.Length > 0)
    {
        JsonSerializer js = new JsonSerializer();
        using (MemoryStream ms = new MemoryStream())
        {
            await file.CopyToAsync(ms);

            using (StreamReader streamReader = new StreamReader(ms))
            {
                CommodityAdditionalCodeTypeDto[] codes= (CommodityAdditionalCodeTypeDto[]) js.Deserialize(streamReader, typeof(CommodityAdditionalCodeTypeDto[]));
            }
        }
   }
   return null;
}

1 个答案:

答案 0 :(得分:4)

小错字。代替

let nd = csharpMethod()
if nd.HasValue then printfn "Date = %A" nd.Value
else printfn "Null date"

写:

<input type="file" name="files" >

您还可以使用<input type="file" name="file" >

访问发送文件
相关问题