I'm trying to realize file uploading to server. I have a form on my html page:
<form name="addForm" asp-action="PostMusic" enctype="multipart/form-data" method="post">
<label for="songName">Song name: </label>
<input type="text" name="songName" />
<label for="artistName">Artist name: </label>
<input type="text" name="artistName" />
<label for="albumName">Album name: </label>
<input type="text" name="albumName" />
<input type="file" name="soundFile" id="soundFile" value="Music file"/>
<input type="file" name="imageFile" id="imageFile" value="Image file"/>
<label for="videoLink">Video link (YouTube): </label>
<input type="url" name="videoLink" />
<label for="categories">Category: </label>
<select name="categories" id="categorySelect">
<option value="all">All</option>
<option value="autorSongs">Author Song</option>
<option value="blues">Blues</option>
<option value="jass">Jass</option>
<option value="country">Country</option>
<option value="classic">Classic</option>
<option value="latinAmerican">Latin American</option>
<option value="metal">Metal</option>
<option value="pop">Pop</option>
<option value="reggi">Reggi</option>
<option value="rhytmNBlues">Rhytm-n-Blues</option>
<option value="rock">Rock</option>
<option value="rap">Rap</option>
<option value="folk">Folk</option>
<option value="hipHop">Hip-Hop</option>
<option value="electroMusic">Electro</option>
</select>
<input type="button" name="addMusicBtn" value="Add"/>
.js file in which I send the data:
var soundFileName = $('#soundFile').get(0).files[0].name;
var soundFile = $('#soundFile').get(0).files[0];
var soundFormData = new FormData();
soundFormData.append(soundFileName, soundFile);
await fetch("../App/music/sound", { method: 'POST', body: soundFormData });
and POST method in my controller:
[HttpPost("sound")]
public void PostMusic([FromForm(Name ="addForm")]IFormFile sound)
{
string path = "../Musics";
string fileName = Path.GetFileName(sound.FileName);
using(FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
sound.CopyTo(stream);
}
}
While sending the file, in JS file is sending, but in PostMusic method field IFormFile sound is null. Thanks in advance for any help.

