简体   繁体   English

是否可以在没有 API 的情况下使用 DxUpload(在 DevExpress Blazor 中)?

[英]Is it possible to use DxUpload (in DevExpress Blazor) without having an API?

I'm using DevExpress components in my Blazor Server App project.我在我的Blazor Server App项目中使用DevExpress组件。 I want to use DxUpload component to upload an excel file to the server and do some bulk insert operations on the database.我想使用DxUpload组件将一个 excel 文件上传到服务器,并对数据库进行一些批量插入操作。 The Documentation says that I need to create a WEB API project. 文档说我需要创建一个WEB API项目。 Do I have to use a separate WEB API while my project is Blazor Server App ?当我的项目是Blazor Server App时,我是否必须使用单独的WEB API

I think this solution is really more of a hack then how DevExpress intends you to use their component.我认为这个解决方案实际上比 DevExpress 希望您如何使用他们的组件更像是一个 hack。 However, you can capture the file upload via the exposed SelectedFilesChanged event and then process the files yourself manually.但是,您可以通过公开的SelectedFilesChanged事件捕获文件上传,然后自己手动处理文件。

<DxUpload UploadMode=UploadMode.OnButtonClick
          SelectedFilesChanged="@SelectedFilesChanged"
          AllowMultiFileUpload="true" 
          @ref="MyUpload" >
</DxUpload>

<DxButton Text="Upload the First File" Click=OnButtonClick />

@code {
    private string FileServerPath => "\\\\server-02\\DATA\\";
    bool UploadVisible { get; set; } = false;
    IEnumerable<UploadFileInfo> Files { get; set; }
    UploadFileInfo FirstFile { get; set; }
    DxUpload MyUpload { get; set; }

    protected void SelectedFilesChanged(IEnumerable<UploadFileInfo> files) {
        Files = files;
        UploadVisible = files.ToList().Count > 0;

        InvokeAsync(StateHasChanged);
    }

    protected async Task OnButtonClick(){
         selectedFile= Files.First();
         Stream stream = selectedFile.OpenReadStream(maxAllowedSize: 50000000);
         FileStream fs = File.Create(FileServerPath + selectedFile.Name));
         await stream.CopyToAsync(fs);
         stream.Close();
         fs.Close();
    }
    StateHasChanged();
}

Obviously the OnButtonClick method needs to be refactored for nullability, using statements, selecting the appropriate file in the collection ect.显然, OnButtonClick方法需要针对可空性、使用语句、在集合中选择适当的文件等进行重构。 but this should point you in the right direction.但这应该为您指明正确的方向。

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

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