小编sgr*_*oft的帖子

如何在上传到服务器 ASP.Net Core 之前从 IFormFile 压缩图像

我有一个问题,我正在 net core 中创建一个应用程序来上传孩子的信息,但是,主要问题是我拥有的所有图像都来自我的手机,你知道我们谈论的是每张图片 9-15 MB ,所以,我知道我不能告诉用户“有一个限制”,但是,我认为它没有用,所以,有一种方法可以减少图像的大小,从而降低质量吗?。

这是我的方法

班级

public IFormFile ImageFile { get; set; }
Run Code Online (Sandbox Code Playgroud)

方法

if (vm.ImageFile != null && vm.ImageFile.Length > 0)
{
    var guid = Guid.NewGuid().ToString();
    var file = $"{guid}.jpg";

    path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images\\Kids", file);

    using (var stream = new FileStream(path, FileMode.Create))
    {
        //var convertedImage = MagicSolutionGivenByTheAwsomeStackOverFlowCommunity
        await vm.ImageFile.CopyToAsync(stream);
    }

}
Run Code Online (Sandbox Code Playgroud)

布局

<form asp-action="Create" enctype="multipart/form-data">
 <input type="hidden" asp-for="Imagen"/>
  <div class="col-sm-4">
   <label asp-for="Imagen" class="control-label"></label>
   <div>
    <input asp-for="ImageFile" class="form-control filestyle"
           type="file" data-classbutton="btn btn-secondary"
           data-classinput="form-control inline"
           data-icon="&lt;span class='fa fa-upload …
Run Code Online (Sandbox Code Playgroud)

c# asp.net .net-core asp.net-core

5
推荐指数
2
解决办法
3708
查看次数

标签 统计

.net-core ×1

asp.net ×1

asp.net-core ×1

c# ×1