英文:
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
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");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论