如何创建 IFormFile 图像文件并将其发送到视图中的输入元素?
How to create an IFormFile image file and send it to input element in view?
我的视图中有一个带有输入元素的表单。
<input asp-for="Image" id="mainImageInput" type="file" onchange="readURL(this,'mainImage')" accept="image/jpeg, image/png" class="form-control"/>
我将我所有的图像作为 byte[] 存储在数据库中。如何将 byte[] 转换为 IFormFile 并将其发送到输入元素中的视图?不,我不是在问如何显示图像,我想将它直接插入到输入元素中,就像用户使用“浏览”选择它一样。
我试过写这个,但它不起作用:
var ad = _context.Advertisements
.Select(x => new {
Id = x.Id,
Title = x.Title,
Description = x.Description,
Price = x.Price,
Location = x.Location,
CategoryId = x.CategoryId,
ImageInBase64 = Convert.ToBase64String(x.Image),
UserId = x.UserId,
ImageInByteArray=x.Image
})
.FirstOrDefault(x=>x.Id==id);
IFormFile file = null;
using (var stream = new MemoryStream(ad.ImageInByteArray))
{
file = new FormFile(stream, 0, ad.ImageInByteArray.Length, "name", "fileName");
}
var viewModel = new AdvertisementViewModel()
{
Id = ad.Id,
Title = ad.Title,
Description = ad.Description,
Price = ad.Price,
Location = ad.Location,
CategoryId = ad.CategoryId,
ImageInBase64 = ad.ImageInBase64,
UserId = ad.UserId,
Image = file
};
return View("Edit",viewModel);
这是提交表单后我应该收到图片的操作,所有其他属性都在那里,但我得到的图片为空。
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public async Task<IActionResult> Edit(AdvertisementViewModel viewModel)
{
// viewmodel.Image is null, so my model state will always be invalid
// need a workaround
if (!ModelState.IsValid)
{
return View(viewModel);
}
}
And no, I am not asking how to display the image, I want to insert it directly into the input element as if the user has selected it using "Browse".
由于浏览器中的安全原因,我认为我们不能将值设置为 <input type='file' />
。
相关帖子,希望对你有用:
1. How to set a value to a file input in HTML?
2. Set default value for a input file form [duplicate]
我的视图中有一个带有输入元素的表单。
<input asp-for="Image" id="mainImageInput" type="file" onchange="readURL(this,'mainImage')" accept="image/jpeg, image/png" class="form-control"/>
我将我所有的图像作为 byte[] 存储在数据库中。如何将 byte[] 转换为 IFormFile 并将其发送到输入元素中的视图?不,我不是在问如何显示图像,我想将它直接插入到输入元素中,就像用户使用“浏览”选择它一样。
我试过写这个,但它不起作用:
var ad = _context.Advertisements
.Select(x => new {
Id = x.Id,
Title = x.Title,
Description = x.Description,
Price = x.Price,
Location = x.Location,
CategoryId = x.CategoryId,
ImageInBase64 = Convert.ToBase64String(x.Image),
UserId = x.UserId,
ImageInByteArray=x.Image
})
.FirstOrDefault(x=>x.Id==id);
IFormFile file = null;
using (var stream = new MemoryStream(ad.ImageInByteArray))
{
file = new FormFile(stream, 0, ad.ImageInByteArray.Length, "name", "fileName");
}
var viewModel = new AdvertisementViewModel()
{
Id = ad.Id,
Title = ad.Title,
Description = ad.Description,
Price = ad.Price,
Location = ad.Location,
CategoryId = ad.CategoryId,
ImageInBase64 = ad.ImageInBase64,
UserId = ad.UserId,
Image = file
};
return View("Edit",viewModel);
这是提交表单后我应该收到图片的操作,所有其他属性都在那里,但我得到的图片为空。
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public async Task<IActionResult> Edit(AdvertisementViewModel viewModel)
{
// viewmodel.Image is null, so my model state will always be invalid
// need a workaround
if (!ModelState.IsValid)
{
return View(viewModel);
}
}
And no, I am not asking how to display the image, I want to insert it directly into the input element as if the user has selected it using "Browse".
由于浏览器中的安全原因,我认为我们不能将值设置为 <input type='file' />
。
相关帖子,希望对你有用:
1. How to set a value to a file input in HTML?
2. Set default value for a input file form [duplicate]