Skip to content

Instantly share code, notes, and snippets.

@lokcito
Created September 15, 2021 20:00
Show Gist options
  • Save lokcito/9ec63afc74067607e7d959a70be32852 to your computer and use it in GitHub Desktop.
Save lokcito/9ec63afc74067607e7d959a70be32852 to your computer and use it in GitHub Desktop.
C# Upload imagen FromForm , Multipart
public class FIleUploadAPI
{
public IFormFile oneimage { get; set; }
public String cellNumber { get; set; }
public String otroCampo { get; set; }
public String otroCampo2 { get; set; }
}
[Route("/api/v1/student/CAU/Ticket/new")]
[HttpPost]
public String postre([FromForm] FIleUploadAPI formdata)
{
if (!ModelState.IsValid)
{
return "Unsuccessful";
}
if (formdata.oneimage.Length > 0)
{
try
{
if (!Directory.Exists("/Users/plaga/Documents/dev/media"))
{
Directory.CreateDirectory("/Users/plaga/Documents/dev/media");
}
using (FileStream filestream = System.IO.File.Create("/Users/plaga/Documents/dev/media/" + formdata.oneimage.FileName))
{
formdata.oneimage.CopyTo(filestream);
filestream.Flush();
return "/Users/plaga/Documents/dev/media/" + formdata.oneimage.FileName;
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
else
{
return "Unsuccessful";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment