float floatValue=22.34555f;
System.out.print(String.format("%.2f", floatValue));
Выход равен 22.35. Если вам нужно 3 десятичных точки, измените его на «% .3f».
Почему вы конвертируете изображение в 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)" />