Upload File+JSON (edit)

ASP.NET Core 2.1 Web API multipart/form-data

https://thomaslevesque.com/2018/09/04/handling-multipart-requests-with-json-and-file-uploads-in-asp-net-core/

https://dev.to/codingdefined/uploading-image-from-angular-to-asp-net-core-web-api-4enm

https://forums.asp.net/t/2099194.aspx?Net+Core+Web+API+How+to+upload+multi+part+form+data

https://blog.kye.dev/restsharp-file-upload/

https://stackoverflow.com/questions/22799392/sending-http-post-multipart-form-data-field-using-restsharp

1. Controller

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading.Tasks;

namespace ApiUploads.Controllers
{
public class StudentModel
{
public string Name { get; set; }
public int Age { get; set; }
public decimal Salary { get; set; }
public bool IsMarried { get; set; }
public IFormFile Image { get; set; }
}

[ApiController]
public class ValuesController : Controller
{
private readonly IHostingEnvironment _env;

public ValuesController(IHostingEnvironment env)
{
_env = env;
}

[HttpPost("api/SaveFile")]
public async Task<IActionResult> SaveFile([FromForm] StudentModel studentModel)
{
string fileName = studentModel.Image.FileName;

string wwwRootFolder = _env.WebRootPath;

string filePath1 = _env.ApplicationName;
string filePath2 = _env.ContentRootPath;
string filePath3 = _env.EnvironmentName;

string filePath = Path.Combine(wwwRootFolder, "Uploads", fileName);

if (studentModel != null && studentModel.Image != null && studentModel.Image.Length > 0)
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await studentModel.Image.CopyToAsync(fileStream);
}
}
return Ok("success");
}
}

2. Test with RestSharp

abc
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;

var caller = new RestSharpCaller("https://localhost:44328/api/");

caller.Post();
}
}

public class RestSharpCaller : ICaller
{
private RestClient client;

public RestSharpCaller(string baseUrl)
{
client = new RestClient(baseUrl);
}

public List<string> GetAllItems()
{
var request = new RestSharp.RestRequest("Default", Method.GET);
IRestResponse response = client.Execute(request);
var responseString = response.Content;
return new List<string>() { responseString };
}

public void Post()
{
// Get file size
string filePath = @"C:\Desktop\Test.xlsx";
FileInfo fi = new FileInfo(filePath);
string fileName = fi.Name;

// Disk
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
var fileContent = br.ReadBytes((int)fi.Length);
br.Close();
fs.Close();

var request = new RestSharp.RestRequest("SaveFile", Method.POST);
request.AlwaysMultipartFormData = true;
var obj = new StudentModel
{
Id = 1,
Name = "Manh",
Salary = 2000,
Age = 36
};
request.AddObject(obj);
request.AddFileBytes("Image", fileContent, fileName, "application/octet-stream");
IRestResponse response = client.Execute(request);
var responseString = response.Content;
}
}

public interface ICaller
{
List<string> GetAllItems();
}

public class StudentModel
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public decimal Salary { get; set; }
public bool IsMarried { get; set; }
public IFormFile Image { get; set; }
}
}