Почему вы конвертируете изображение в JSON?
Вы передаете свой файл непосредственно в службу. Это идея. Контроллер получает отправленный файл POST
BACKEND
using System.Net.Http.Headers;
[HttpPost]
public ActionResult UploadFile()
{
try
{
var file = Request.Form.Files[0];
if (file.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName;
string fullPath = Path.Combine(@"C:\wwwroot\upload", fileName);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
}
}
return Json("OK");
}
catch (System.Exception ex)
{
return Json("Failed");
}
}
FRONTEND
export class UploadComponent {
constructor(private http: HttpClient) { }
upload(files) {
if (files.length === 0)
return;
const formData = new FormData();
for (let file of files)
formData.append(file.name, file);
const uploadReq = new HttpRequest('POST', `api/uploadFile`, formData);
this.http.request(uploadReq).subscribe(event => {
if (event.type === HttpEventType.Response)
//DONE
});
}
}
HTML
<input #file type="file" (change)="upload(file.files)" />
Это была ошибка в старых версиях рыбы, которая была исправлена в 2.6.0 (выпущена в июне 2017 года).