Skip to content

Instantly share code, notes, and snippets.

@amankkg
Created November 20, 2017 15:44
Show Gist options
  • Save amankkg/233ba774795b4dfe09ee827a25506961 to your computer and use it in GitHub Desktop.
Save amankkg/233ba774795b4dfe09ee827a25506961 to your computer and use it in GitHub Desktop.
asp.net core, accept file and form
[HttpPost]
public async Task<IActionResult> Create([FromForm] UploadItem data)
{
var files = Request.Form.Files;
if (files.Count < 1) return BadRequest(new {message = nameof(Resource.NoFilesAttached)});
await _svc.CreateAsync(files, data, User.GetUserId());
return Ok();
}
public class UploadItem
{
[Required, Range(1, int.MaxValue)]
public int DataSourceId { get; set; }
public string Description { get; set; }
}
const formData = new FormData()
formData.append('datafile', file, file.name)
formData.append('DataSourceId', this.state.dataSourceId)
formData.append('Description', this.state.description)
const xhr = new XMLHttpRequest()
xhr.open('post', '/api/endpoint', true)
xhr.send(formData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment