我IFormFile在类库中使用,Microsoft.AspNetCore.Http.Features已被弃用。我现在需要什么样的块包,用于IFormFile.Net 7
小智 13
请阅读此文档:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/target-aspnetcore
删除 .csproj 文件中的 Microsoft.Extensions.Features 等任何包,然后引用框架。
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
我在类库中使用 IFormFile,Microsoft.AspNetCore.Http.Features 已被弃用。我现在需要什么样的块包,用于 .Net 7 中的 IFormFile
实际上,.NET 7 中已经默认自动集成了IFormFile 。此外,如果您使用 IFormFile 进行流式传输任何文件操作,则不再需要额外的包,因为属于Microsoft.AspNetCore.Http.Features.dll程序集的 IFormFile 和Microsoft.AspNetCore.Http namaspace 已包含在 .Net 7 中。 ,您不需要为此安装或包含任何额外的 nuget 包。
您可以查看以下信息:
如何在类库中使用:
为了在 .NET 7 的类库中使用 IFormFile,我们可以使用 Microsoft.AspNetCore.Http nuget 包。尽管它超出了支持范围,但仍然可以使用,因为 asp.netcore 2.2 被视为独特的类别并延续了其遗产。
例子:
namespace IFromFileTestClassLibrary
{
public class UploadFiles
{
public async Task<string> FileHandelerMethod(CreateProfileViewModel requestModel)
{
string filePath = "file saving path";
var path = Path.Combine(filePath, "Images", requestModel.Photo!.FileName);
if (requestModel.Photo.Length > 0)
{
using var stream = new FileStream(path, FileMode.Create);
await requestModel.Photo.CopyToAsync(stream);
}
return requestModel.ImageURL = path;
}
}
public class CreateProfileViewModel
{
public string? ProfileName { get; set; }
public string? DisplayPhoto { get; set; }
public string? ImageURL { get; set; }
public IFormFile? Photo { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
注:更多详情可以参考官方文档。
| 归档时间: |
|
| 查看次数: |
4666 次 |
| 最近记录: |