简体   繁体   English

如何使用 Blazor 服务器上传图像

[英]How do I upload an image using Blazor Server

so I am new to Blazor and kinda have a little experience in C#.所以我是 Blazor 的新手,对 C# 有点经验。 I am trying to upload an image to my database and can't seem to figure out what I am doing wrong.我正在尝试将图像上传到我的数据库,但似乎无法弄清楚我做错了什么。 Here is my "BlogCreate" file where I can create a blog.这是我可以创建博客的“BlogCreate”文件。

                <div class="form-group">
                <label for="City" class="control-label">Context</label>
                <input form="City" class="form-control" @bind="@blog.Context" />
            </div>
            <InputFile OnChange="@LoadFiles" multiple />

        </div>
    </div>

    <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <input type="button" class="btn btn-primary" @onclick="@CreateBlog" value="Save"/>
                <input type="button" class="btn btn-primary" @onclick="@Cancel" value="Cancel"/>
            </div>
        </div>
    </div>
</form>

@code {
    Blog blog = new Blog();
    public byte[] ImgUpload { get; set; }
    protected async void CreateBlog()
    {
        await blogService.InsertBlog(blog);
        NavigationManager.NavigateTo("blogs");
    }

    void Cancel()
    {
        NavigationManager.NavigateTo("blogs");
    }

    private void LoadFiles(InputFileChangeEventArgs e, BlogService.ImgUpload)
    {
             selectedFiles = e.GetMultipleFiles();
     message = $"{selectedFiles.Count} file(s) selected";
     this.StateHasChanged();
    }
}

I tried copying some stuff from Microsofts docs, but I honestly couldn't figure out how to use it.我试着从微软的文档中复制一些东西,但老实说我不知道如何使用它。 So here is my services code basically functions for getting the blogs and deleting:所以这是我的服务代码,基本上是获取博客和删除的功能:

            //Get blogs
        public async Task<List<Blog>> GetAllBlogs()
        {
            return await _Context.Blogs.ToListAsync();
        }

        //Create blogs
        public async Task<bool> InsertBlog(Blog blog)
        {
            await _Context.Blogs.AddAsync(blog);
            await _Context.SaveChangesAsync();
            return true;

        }

and lastly my tables in my database:最后是我数据库中的表:

            [Key]
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        [Required]
        public string Author { get; set; }
        [Required]
        public string Description { get; set; }
        [Required]
        public string Context { get; set; }
        public string Imgname { get; set; }
        public byte[] Img { get; set; }

can someone tell me what I'm doing wrong I am struggling to figure out what direction to turn to upload an image to my database.有人可以告诉我我做错了什么我正在努力弄清楚将图像上传到我的数据库的方向。 I am using entity framework as well.我也在使用实体框架。

You need to do something in your handler when files are selected.选择文件时,您需要在处理程序中执行某些操作。 This is like remembering the files selected by the user for when the Submit button is pressed.这就像在按下提交按钮时记住用户选择的文件。 Here's an example:这是一个例子:

  private void LoadFiles(InputFileChangeEventArgs e, BlogService.ImgUpload)
    {
             selectedFiles = e.GetMultipleFiles();
     message = $"{selectedFiles.Count} file(s) selected";
     this.StateHasChanged();

          // you need to do something here with the files, like add them to your Blog            
          var file = selectedFiles.First();
          blog.Img = ReadFileBytes(file); // (you can use file.OpenReadStream)
          blog.Imgname = file.Name;
    }

As currently coded, you will create a Blog but never assign the Img/Imgname properties to the blog below.按照目前的编码,您将创建一个Blog ,但从不将Img/Imgname属性分配给下面的blog

protected async void CreateBlog()
{
    await blogService.InsertBlog(blog);
    NavigationManager.NavigateTo("blogs");
}

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

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