asp.net core MVC - 框架net6.0
我有一個頁面,我在其中上傳影像并將其保存到資料庫。我從視圖中獲取的檔案是 IFormFile。我希望能夠在保存到資料庫之前檢查照片的解析度(寬度和高度)。可以用 IFormFile 完成嗎?
這是處理檔案的控制器:
public JsonResult Submit(IFormFile PhotoFile)
{
int success = 0;
string excep = "";
try
{
if (PhotoFile.Length > 0)
{
using (var ms = new MemoryStream())
{
PhotoFile.CopyTo(ms);
var fileBytes = ms.ToArray();
}
}
ApplicationUser appUser =
_unitOfWork.ApplicationUser.GetAll().Where(a => a.UserName == User.Identity.Name).FirstOrDefault();
if (appUser != null)
{
FileUpload fileUpload = new FileUpload()
{
file = PhotoFile,
CompanyId = appUser.CompanyId
};
SaveFile(fileUpload);
}
excep = "success";
success = 1;
return Json(new { excep, success });
}
catch (Exception ex)
{
excep = "fail";
success = 0;
return Json(new { excep, success });
}
}
public string SaveFile(FileUpload fileObj)
{
Company company = _unitOfWork.Company.GetAll().
Where(a => a.Id == fileObj.CompanyId).FirstOrDefault();
if(company != null && fileObj.file.Length > 0)
{
using (var ms = new MemoryStream())
{
fileObj.file.CopyTo(ms);
var fileBytes = ms.ToArray();
company.PhotoAd = fileBytes;
_unitOfWork.Company.Update(company);
_unitOfWork.Save();
return "Saved";
}
}
return "Failed";
}
uj5u.com熱心網友回復:
據我所知,這是不可能的IFormFile,你需要System.Drawing.Common。所以首先你需要像這樣轉換它:
using var image = Image.FromStream(PhotoFile.OpenReadStream());
然后你可以簡單地得到高度/寬度image.height和image.width
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/424692.html
