【问题标题】:Uploading File and Employee Model in POST Web API在 POST Web API 中上传文件和员工模型
【发布时间】:2020-07-13 16:01:36
【问题描述】:

我有一个员工模型和头像。我需要在一种 POST 方法中上传个人资料图片和模型日期。我只需要将图像文件名保存在数据库中并在 WebRoot 目录中上传图像。

这是我的模型: 公共部分类雇主

{
    public int EmployerId { get; set; }
    public string Companyname { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Mobile { get; set; }
    public string Password { get; set; }
    public string DisplayImage { get; set; }
    public bool? IsActive { get; set; }
    public DateTime StateDate { get; set; }
}

这是我的控制器代码:

    [HttpPost]
    public async Task<ActionResult<Employees>> PostEmployees([FromForm] FileUploadAPI Image, [FromBody]Employees employees)
    {
        try
        {
            _context.Employees.Add(employees);
            await _context.SaveChangesAsync();
            await UploadImage(Image, 2, employees.EmployeeId);
            var returnInfo = CreatedAtAction("GetEmployees", new { id = employees.EmployeeId }, employees);
            return returnInfo;
        }
        catch(Exception ex)
        {
            return NoContent();
        }
    }

    public class FileUploadAPI
    {
        public IFormFile files { get; set; }
    }

    public async Task<string> UploadImage(FileUploadAPI files, int UserType, int UserId)
    {
        if (files.files.Length > 0)
        {
            try
            {
                if (!Directory.Exists(_hostingEnvironment.WebRootPath + "\\Employees\\"))
                {
                    Directory.CreateDirectory(_hostingEnvironment.WebRootPath + "\\Employees\\");
                }
                Guid guid = Guid.NewGuid();
                string filename = _hostingEnvironment.WebRootPath + "\\Employees\\" + $"EM-{UserType}-UserId-{guid}";
                using (FileStream filestream = System.IO.File.Create(filename))
                {
                    await files.files.CopyToAsync(filestream);
                    filestream.Flush();
                    return filename;
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        else
        {
            return "Not Found";
        }
    }

如果我只是在没有员工模型的情况下在 POSTMAN 中上传文件,它工作正常。但是当我同时传递两个文件员工数据时,文件返回空值。

任何建议,解决方案?

谢谢

【问题讨论】:

  • 嗨@Jognu Khan,我的回答对您解决问题有帮助吗?如果是。请您接受作为答案吗?参考:How to accept as answer。如果不是,请跟进让我知道。

标签: asp.net-core


【解决方案1】:

here 所述,不可能同时使用[FromForm][FromBody]。但我认为你有两个选择:

您可以将 JSON 正文放入表单并发送 Employee 数据以及 File 或使用 2 个单独的端点进行表单上传。一个端点用于使用[FromFile] 上传用户图片并获取pictureId 和另一个用于在正文中发送Employee 并填充pictureId 键。

【讨论】:

  • 能否请您帮我建议如何将这个文件上传到 POSTMAN(表单数据)中。实际上,我无法通过 POST 操作访问该文件。
【解决方案2】:

首先将FromBody 更改为FromForm。然后,因为您想将文件名保存到数据库,请更改您的代码如下:

[HttpPost]
public async Task<ActionResult<Employers>> PostEmployees([FromForm] FileUploadAPI Image, [FromForm]Employers employees)
{
    try
    {                
        var filename = await UploadImage(Image, 2, employees.EmployerId);
        employees.DisplayImage = filename;
        _context.Employers.Add(employees);
        await _context.SaveChangesAsync();
        var returnInfo = CreatedAtAction("GetEmployees", new { id = employees.EmployerId }, employees);
        return returnInfo;
    }
    catch (Exception ex)
    {
        return NoContent();
    }
}

您的邮递员应如下所示:

结果:

【讨论】:

    猜你喜欢
    • 2016-07-02
    • 2021-08-09
    • 2012-09-27
    • 1970-01-01
    • 2017-05-13
    • 2016-09-16
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    相关资源
    最近更新 更多