ByteArray to IFormFile

问题描述 投票:0回答:1

我正在使用C#和Net Core开发一些REST API我的存储库中有一个函数可以接受IFormFile类型的参数。

public async Task<bool> UploadFile(IFormFile file)
{
    // do some stuff and save the file to azure storage
}

[此函数由控制器方法调用,该方法将上传的文件传递给它

public class FileController : Controller
{
    public async Task<IActionResult> UploadDoc(IFormFile file
    {
        // Call the repository function to save the file on azure
        var res = await documentRepository.UploadFile(file);
    }
}

现在我有另一个函数,该函数调用外部API,该API返回文件作为字节数组。我想使用repository.UploadFile方法保存此字节数组,但无法将字节数组对象转换为IFormFile。有可能吗?

c# asp.net-core file-upload
1个回答
0
投票

您可以将字节数组转换为MemoryStream

var stream = new MemoryStream(byteArray);

..然后将其传递给constructor类的FromFile

FromFile
© www.soinside.com 2019 - 2024. All rights reserved.