Created
November 20, 2017 15:44
-
-
Save amankkg/233ba774795b4dfe09ee827a25506961 to your computer and use it in GitHub Desktop.
asp.net core, accept file and form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UploadItem | |
{ | |
[Required, Range(1, int.MaxValue)] | |
public int DataSourceId { get; set; } | |
public string Description { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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