繁体   English   中英

.NET 7 System.IO.FileNotFoundException:找不到文件

[英].NET 7 System.IO.FileNotFoundException: Could not find file

这是我第一次在这里问,如果措辞不好,请见谅。

我有一个带有 MudBlazor 的 blazor WebAssembly 项目,当我尝试上传文件以将它们保存到数据库中时,它会在浏览器控制台中显示下一条错误消息。
System.IO.FileNotFoundException: Could not find file

当用户上传文件时,我调用下一个方法将文件保存到IList<IBrowserFile>中。

IList<IBrowserFile> Files = new List<IBrowserFile>();
private void OnInputFileChanged(InputFileChangeEventArgs e)
    {
        var files = e.GetMultipleFiles();
        foreach(var file in files)
        {
            Files.Add(file);
        }
    }

一旦用户上传了所有文件,他们点击一个按钮调用下一个方法将其上传到数据库中。

[Inject] protected ISnackbar Snackbar { get; set; } = default!;
private async void Upload()
    {

        List<string>? notUploadFiles = null;
        foreach(var file in Files)
        {
            byte[] fileBytes = File.ReadAllBytes(destPath + file.Name);
            string extn = new FileInfo(file.Name).Extension;

            var addArchivoTarea = new AddArchivoTareaRequestDTO(Tarea.Id, fileBytes, extn);
            var successResponse = await HttpTareas.AddArchivoToTareaAsync(addArchivoTarea);

            if (!successResponse)
            {
                notUploadFiles.Add(file.Name);
            }
        }

        if(notUploadFiles is not null) {
            Snackbar.Configuration.SnackbarVariant = Variant.Filled;
            Snackbar.Add("The following files could not be uploaded:", Severity.Info);

            Snackbar.Configuration.SnackbarVariant = Variant.Outlined;
            foreach (var file in notUploadFiles)
            {
                Snackbar.Add(file, Severity.Error);
            }

            //Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
            //Snackbar.Add("TODO: Upload your files!", Severity.Normal);
            MudDialog.Close(DialogResult.Ok(true));
        }

        Snackbar.Add("All files have been successfully uploaded", Severity.Success);
        MudDialog.Close(DialogResult.Ok(true));
    }

我不知道问题出在哪里,有什么想法吗?

上传的文件不在文件系统中,它们只在内存中。 访问原始数据的方式类似于:

        byte[] fileBytes;

        using (Stream s = file.OpenReadStream())
        {
            MemoryStream ms = new MemoryStream();
            await s.CopyToAsync(ms);
            fileBytes= ms.ToArray();
        }

暂无
暂无

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

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