Posting Model WEB API NET CORE 6

huangapple go评论59阅读模式
英文:

Posting Model WEB API NET CORE 6

问题

public partial class Thongtin
{
    public string? Id { get; set; }
    public string? Hovaten { get; set; }
    public string? Sodienthoai { get; set; }
    public string? Diachilapdat { get to; set; }
    public string? Tenhinhanh { get; set; }
    public string? Hinhanh { get; set; }
}

public IActionResult Upload(IFormFile file, Thongtin a)
{
    if (file != null)
    {
        var uniqueFileName = GetUniqueFileName(file.FileName);

        var uploads = Path.Combine(Environment.WebRootPath, "Uploads");
        var filePath = Path.Combine(uploads, uniqueFileName);
        file.CopyTo(new FileStream(filePath, FileMode.Create));
    }

    return Ok("upload successful");
}

public IActionResult Upload(IFormFile file)
{
    if (file != null)
    {
        var uniqueFileName = GetUniqueFileName(file.FileName);

        var uploads = Path.Combine(Environment.WebRootPath, "Uploads");
        var filePath = Path.Combine(uploads, uniqueFileName);
        file.CopyTo(new FileStream(filePath, FileMode.Create));
    }

    return Ok("upload successful");
}

Because I want to save text and image name to the database and file image to the source code folder.

I want to get a form upload and an object at the same time in a POST method.

Please note that I've only translated the code parts as requested, and I haven't provided any additional information or answered your specific question.

英文:
public partial class Thongtin
{
    public string? Id { get; set; }

    public string? Hovaten { get; set; }

    public string? Sodienthoai { get; set; }

    public string? Diachilapdat { get; set; }

    public string? Tenhinhanh { get; set; }

    public string? Hinhanh { get; set; }
}

 public IActionResult Upload(IFormFile file, Thongtin a )
        {
            if (file != null)
            {
                var uniqueFileName = GetUniqueFileName(file.FileName);


                var uploads = Path.Combine(Environment.WebRootPath, "Uploads");
                var filePath = Path.Combine(uploads, uniqueFileName);
                file.CopyTo(new FileStream(filePath, FileMode.Create));
            }
        


            return Ok("upload successfull");
        }

This is my reseult when i put object and type IFORMFile
Posting Model WEB API NET CORE 6

The result not have object
Posting Model WEB API NET CORE 6

 public IActionResult Upload(  IFormFile file)
        {
            if (file != null)
            {
                var uniqueFileName = GetUniqueFileName(file.FileName);


                var uploads = Path.Combine(Environment.WebRootPath, "Uploads");
                var filePath = Path.Combine(uploads, uniqueFileName);
                file.CopyTo(new FileStream(filePath, FileMode.Create));
            }
        


            return Ok("upload successfull");
        }

Because i want to save text and image name to database and file image to forder source code

I want to get form upload and object same time in method post

答案1

得分: 0

你可以使用IFormFile类型扩展Thongtin类。然后,在API中使用[Consumes("multipart/form-data")][FromForm]属性进行参数绑定。

public class Thongtin
{
    public string? Id { get; set; }
    public IFormFile? file { get; set; }
    public string? Hovaten { get; set; }
    public string? Sodienthoai { get; set; }
    public string? Diachilapdat { get; set; }
    public string? Tenhinhanh { get; set; }
    public string? Hinhanh { get; set; }
}

然后,你可以在API方法中使用以下方式上传文件和参数:

[HttpPost]
[Consumes("multipart/form-data")]
public IActionResult Upload([FromForm] Thongtin a)
{
    if (a.file != null)
    {
        var uniqueFileName = GetUniqueFileName(a.file.FileName);
        var uploads = Path.Combine(Environment.WebRootPath, "Uploads");
        var filePath = Path.Combine(uploads, uniqueFileName);
        a.file.CopyTo(new FileStream(filePath, FileMode.Create));
    }
    return Ok("上传成功");
}

然后,你可以同时上传文件和参数。

英文:

You can extend the Thongtin class with the IFormFile type. Then using 1 parametr for the API with [Consumes("multipart/form-data")] and [FromForm] attribute.

public class Thongtin
    {      
        public string? Id { get; set; }
        public IFormFile? file { get; set; }
        public string? Hovaten { get; set; }
        public string? Sodienthoai { get; set; }
        public string? Diachilapdat { get; set; }
        public string? Tenhinhanh { get; set; }
        public string? Hinhanh { get; set; }
    }
        [HttpPost]
        [Consumes("multipart/form-data")]
        public IActionResult Upload([FromForm] Thongtin a)
        {
            if (a.file != null)
            {
                var uniqueFileName = GetUniqueFileName(a.file.FileName);
                var uploads = Path.Combine(Environment.WebRootPath, "Uploads");
                var filePath = Path.Combine(uploads, uniqueFileName);
                a.file.CopyTo(new FileStream(filePath, FileMode.Create));
            }
            return Ok("upload successfull");
        }

Then you can post file and parameters at the same time<br>
Posting Model WEB API NET CORE 6
Posting Model WEB API NET CORE 6

huangapple
  • 本文由 发表于 2023年2月27日 17:39:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定