簡體   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