Answer the question
In order to leave comments, you need to log in
How to implicitly convert type "Microsoft.AspNetCore.Http.IFormFile" to "byte[]"?
Hello, I want to upload drag-and-drop images, but I got the error "CS0029 C# Cannot implicitly convert type 'Microsoft.AspNetCore.Http.IFormFile' to 'byte[]'."
@page
@model MyWebSIte.Pages.Company.CreateModel
@{
ViewData["Title"] = "Create";
}
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
<link rel="stylesheet" href="/css/create.css">
<div class="container-fluid">
<div class="container">
<div class="row">
<div class="col">
<h1>@Localizer["Создание"]</h1>
<h4>@Localizer["Компании"]</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Company.Title" class="control-label"></label>
<input asp-for="Company.Title" class="form-control" [email protected]["Название"] />
<span asp-validation-for="Company.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.ReleaseDate" class="control-label"></label>
<input asp-for="Company.ReleaseDate" class="form-control" />
<span asp-validation-for="Company.ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.Rating" class="control-label"></label>
<input asp-for="Company.Rating" class="form-control" [email protected]["Рейтинг"] />
<span asp-validation-for="Company.Rating" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.Bonus" class="control-label"></label>
<input asp-for="Company.Bonus" class="form-control" [email protected]["Бонус"] />
<span asp-validation-for="Company.Bonus" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.Description" class="control-label"></label>
<input asp-for="Company.Description" class="form-control" [email protected]["Описание"] />
<span asp-validation-for="Company.Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.Thematics" class="control-label"></label>
<input asp-for="Company.Thematics" class="form-control" [email protected]["Тематика"] />
<span asp-validation-for="Company.Thematics" class="text-danger"></span>
</div>
<div class="form-group">//загрузка изображений
<div id="dropZone">
Для загрузки, перетащите файл сюда.
</div>
</div>
<div class="form-group">
<label asp-for="Company.Video" class="control-label"></label>
<input asp-for="Company.Video" class="form-control" [email protected]["Видео"] />
<span asp-validation-for="Company.Video" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.Topic" class="control-label"></label>
<input asp-for="Company.Topic" class="form-control" [email protected]["Тема"] />
<span asp-validation-for="Company.Topic" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.News" class="control-label"></label>
<input asp-for="Company.News" class="form-control" [email protected]["Новости"] />
<span asp-validation-for="Company.News" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.Price" class="control-label"></label>
<input asp-for="Company.Price" class="form-control" [email protected]["Цена"] />
<span asp-validation-for="Company.Price" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Company.Tags" class="control-label"></label>
<input asp-for="Company.Tags" class="form-control" [email protected]["Тэги"] />
<span asp-validation-for="Company.Tags" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">@Localizer["Вернуться к списку"]</a>
</div>
</div>
</div>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
using Microsoft.AspNetCore.Http;
namespace MyWebSIte.Models
{
public class CompanyViewModel
{
public IFormFile Images { get; set; }
}
}
using Microsoft.AspNetCore.Mvc;
using MyWebSIte.Data;
using MyWebSIte.Models;
using System.IO;
using System.Linq;
namespace MyWebSIte.Pages
{
public class HomeController : Controller
{
ApplicationDbContext _context;
public HomeController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult Index()
{
return View(_context.Company.ToList());
}
[HttpPost]
public IActionResult Create(CompanyViewModel pvm)
{
Models.Company company = new Models.Company { Images = pvm.Images };// здесь возникает ошибка
if (pvm.Images != null)
{
byte[] imageData = null;
// считываем переданный файл в массив байтов
using (var binaryReader = new BinaryReader(pvm.Images.OpenReadStream()))
{
imageData = binaryReader.ReadBytes((int)pvm.Images.Length);
}
// установка массива байтов
company.Images = imageData;
}
_context.Company.Add(company);
_context.SaveChanges();
return RedirectToAction("Index");
}
}
}
Answer the question
In order to leave comments, you need to log in
You can't implicitly - you can only explicitly.
// где-то выше IFormFile file
using var fileStream = file.OpenReadStream();
byte[] bytes = new byte[file.length];
fileStream.Read(bytes, 0, (int)file.Length);
// теперь в bytes лежит наш файл
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question