【问题标题】:Upload files in Blazor is not going to the uploads folder in wwwrootBlazor 中的上传文件不会转到 wwwroot 中的上传文件夹
【发布时间】:2020-02-10 22:39:36
【问题描述】:

在我的 Blazor 应用程序中,我有一个 wwwroot/upload 文件夹,我想在其中上传图像。但是图像永远不会进入目录。

我的代码 -

async Task UploadFile()
{

    if (file != null)
    {
        // Just load into .NET memory to show it can be done
        // Alternatively it could be saved to disk, or parsed in memory, or similar
        var ms = new System.IO.MemoryStream();
        await file.Data.CopyToAsync(ms);

        status = $"Finished loading {file.Size} bytes from {file.Name}";

        // TODO: Need to upload the file to wwwroot/upload
        var content = new MultipartFormDataContent {
          { new ByteArrayContent(ms.GetBuffer()), "\\upload\\", file.Name }
        };


        await Http.PostAsync("api/Blog/UploadFeaturedImage", content);
    }
}

为什么文件没有到达目的地?

更新

我的 api/Blog/UploadFeaturesImage 在下面,我相信问题出在此处。它将所有文件上传到 API 项目中名为 Upload 的文件夹中。请问如何告诉它上传到 wwwroot/upload?

[Authorize]
    [HttpPost("[action]")]
    public async Task UploadFeaturedImage()
    {
        if (HttpContext.Request.Form.Files.Any())
        {
            foreach (var file in HttpContext.Request.Form.Files)
            {
                var path = Path.Combine(Environment.CurrentDirectory, "upload", file.FileName);
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }
            }
        }
    }

【问题讨论】:

  • 控制台日志有错误吗?
  • 你如何处理“api/Blog/UploadFeaturedImage”上的文件?
  • @aguafrommars 现在我添加了 api 代码,我可以看到问题,它上传到服务器项目上的文件夹,而不是 Blazor 客户端项目。我可以在那里的上传文件夹中看到上传。我将如何指定 wwwroot 文件夹而不是服务器文件夹?
  • 通过自定义其路径,但在 Prod 环境中,您是否将为您的 blazor 应用程序创建一个文件夹?我猜没有。
  • @aguafrommars 可能不是。实现这一目标的最佳方法是什么?将图像存储在服务器上的位置是有意义的,但是当我需要在 Blazor 应用程序中显示图像时如何引用它?

标签: blazor


【解决方案1】:

不确定这是否会对您有所帮助,但我有一个名为 BlazorFileUpload 的 Nuget 包,它带有一个示例项目:

https://github.com/DataJuggler/BlazorFileUpload

这是基于 Microsoft Blazor 的创建者 SteveSanderson 的 BlazorInputFile。

剃刀代码:

@使用 DataJuggler.Blazor.FileUpload

<FileUpload CustomSuccessMessage="Your file uploaded successfully." 
    OnChange="OnFileUploaded" FilterByExtension="true" 
    ShowStatus="false"  PartialGuidLength="10" MaxFileSize="4194304"        
    ShowCustomButton="true" ButtonText="Select File" 
    CustomButtonClassName="buttonwide"        
    AllowedExtensions=".jpg;.png;" ShowResetButton="true" OnReset="OnReset" 
    ResetButtonClassName="button" ResetButtonText="Reset"
    CustomExtensionMessage="Only .jpg and .png files are allowed." 
    AppendPartialGuid="true"                
    FileTooLargeMessage="The file uploaded must be 4 megabytes or smaller.">
</FileUpload>

文件上传后调用OnFileUploaded事件回调:

/// <summary>
/// method returns the File Uploaded
/// </summary>
private void OnFileUploaded(UploadedFileInfo uploadedFileInfo)
{
    // if aborted
    if (uploadedFileInfo.Aborted)
    {
        // get the status
        status = uploadedFileInfo.ErrorMessage;
    }
    else
    {
        // get the status
        status = "The file " + uploadedFileInfo.FullName + " was uploaded.";

        // other information about the file is available
        //DateTime lastModified = uploadedFileInfo.LastModified;
        //string nameAsItIsOnDisk = uploadedFileInfo.NameWithPartialGuid;
        //string partialGuid = uploadedFileInfo.PartialGuid;
        //long size = uploadedFileInfo.Size;
        //string type = uploadedFileInfo.Type;

        // The above information can be used to display, and / or process a file
    }
}

示例项目位于上面列出的 Git Repo 的 Samples 文件夹中。

FileUpload 组件默认的 UploadDirectory 是 'wwwroot/Upload',所以它应该适合你:

[Parameter]
public string UploadFolder { get; set; } = "wwwroot/Upload/";

我几乎完成了 BlazorImageGallery 示例,应该在本周晚些时候完成:

如果有人想观看,BlazorFileUpload 项目还附带一个视频:https://youtu.be/Hj_k90mifSQ

也许它会对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2017-10-03
    • 2016-03-11
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多