繁体   English   中英

如何从 Blazor 导入 Excel 文件?

[英]How can I import Excel-files from Blazor?

我有一个网络应用程序。 有一个导入Excel工作簿的function。 我想导入一个或多个 52 页(周)Excel 工作簿的内容。 我做错了什么? 有谁能够帮我?

  • 为什么 ContentType/ContentDisposition 是 null 参考。 我该如何解决这个问题?
  • “名称”的内容必须是什么? 这与文件名相同吗?
  • 服务中显示空引用的错误消息: var data = JsonConvert.SerializeObject(model); .

在 razor 页面中,您有两个按钮 Select 和 Import

                    <div class="d-inline">
                        <label for="file" class="btn myButtonClass">
                            selecteer
                        </label>
                        <InputFile class="btn myButtonClass"
                               id="file" multiple
                               OnChange="@OnInputFileChange" />
                    </div> 

@code
{
    ...
    private IReadOnlyList<IBrowserFile> list;

    private async Task OnInputFileChange(InputFileChangeEventArgs e)
    {
        list = e.GetMultipleFiles();

        await _JsRuntime.InvokeAsync<string>("updateList");
    }

    public async Task DoImport()
    {
        _model.MinWeek = WeekFrom;
        _model.MaxWeek = WeekTo;

        foreach (var item in list)
        {
            Stream stream = item.OpenReadStream(Int64.MaxValue);
            var file = new FormFile(stream, 0, stream.Length, "\"files\"", item.Name);
            //file.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            //file.ContentDisposition= "x-file-name";

            _model.ImportFile.Add(file);
        }

        await _listService.Import(_model);
    }

在服务中:

public async Task<string> ImportHitlijst(ImportSelectModel model)
{
    try
    {
        var data = JsonConvert.SerializeObject(model);

        HttpContent content = new StringContent(data, Encoding.UTF8, "application/json");

        var httpResponseMessage = await _httpClient.PostAsync($"api/Hitlijst/SubmitFiles", content);

        if (httpResponseMessage.IsSuccessStatusCode)
        {
            return "Saved successful";
        }

        return "Not saved";
    }
    catch (Exception ex)
    {
        string message = ex.Message;

        return "Error with message: " + message;
    }
}

...然后在 API 中:

        [HttpPost("SubmitFiles")]
        [RequestSizeLimit(long.MaxValue)]
        public async Task<IActionResult> SubmitFiles([FromForm] ImportSelectModel model)
        {
            if (model.ImportFile.Count > 0)
            {
...

我使用 model:

 public class ImportSelectModel
    {
        public int MinWeek { get; set; }
        public int MaxWeek { get; set; }
        [Required] public List<IFormFile> ImportFile { get; set; } = new();
    }

任何建议表示赞赏。 谢谢!

在此处输入图像描述

您必须定义 Headers 和 ContentType:

 public async Task OnInputFileChange(InputFileChangeEventArgs e)
    {
        var files = e.GetMultipleFiles(int.MaxValue);

        foreach (var file in files)
        {
            IBrowserFile uploadedFile = file;
            Stream stream = file.OpenReadStream(Int64.MaxValue);

            var formFile = new FormFile(stream, 0, stream.Length, null, file.Name)
                {
                    Headers = new HeaderDictionary(),
                    ContentType = uploadedFile.ContentType
                };

            _model.ImportFile.Add(formFile);
            Bestandnamen.Add(file.Name);
        }

        StateHasChanged();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM