从 IFormFile 异常中获取内存流
Get memory stream from IFormFile exceptions
我上传了一张图片,想将其发送到第三方服务 (Cloudinary),而不将文件保存在我的服务器中。
public async Task<List<string>> GetImagesUrlsByImage(IFormFile image)
{
List<string> urlList = new List<string>();
ImageUploadParams uploadParams = new ImageUploadParams();
using (var memoryStream = new MemoryStream())
{
await image.CopyToAsync(memoryStream);
uploadParams.File = new FileDescription(image.FileName, memoryStream);
uploadParams.EagerTransforms = new List<Transformation>
{
new EagerTransformation().Width(200).Height(150).Crop("scale"),
new EagerTransformation().Width(500).Height(200).Crop("scale")
};
ImageUploadResult result = await _cloudinary.UploadAsync(uploadParams);
var url = result.SecureUrl.ToString();
urlList.Add(url);
}
return urlList;
}
我没有收到异常,但来自 Cloudinary 的结果消息有一条错误消息:"No image";
调试时我看到这些错误:
我需要在此代码中修复什么?
最有可能的是,假设其他一切正常,您只需要在 MemoryStream
:
中重置光标位置
ms.Position = 0;
如此完整的示例:
public async Task<List<string>> GetImagesUrlsByImage(IFormFile image)
{
List<string> urlList = new List<string>();
ImageUploadParams uploadParams = new ImageUploadParams();
using (var memoryStream = new MemoryStream())
{
await image.CopyToAsync(memoryStream);
ms.Position = 0; // set cursor to the beginning of the stream.
uploadParams.File = new FileDescription(image.FileName, memoryStream);
uploadParams.EagerTransforms = new List<Transformation>
{
new EagerTransformation().Width(200).Height(150).Crop("scale"),
new EagerTransformation().Width(500).Height(200).Crop("scale")
};
ImageUploadResult result = await _cloudinary.UploadAsync(uploadParams);
var url = result.SecureUrl.ToString();
urlList.Add(url);
}
return urlList;
}
我上传了一张图片,想将其发送到第三方服务 (Cloudinary),而不将文件保存在我的服务器中。
public async Task<List<string>> GetImagesUrlsByImage(IFormFile image)
{
List<string> urlList = new List<string>();
ImageUploadParams uploadParams = new ImageUploadParams();
using (var memoryStream = new MemoryStream())
{
await image.CopyToAsync(memoryStream);
uploadParams.File = new FileDescription(image.FileName, memoryStream);
uploadParams.EagerTransforms = new List<Transformation>
{
new EagerTransformation().Width(200).Height(150).Crop("scale"),
new EagerTransformation().Width(500).Height(200).Crop("scale")
};
ImageUploadResult result = await _cloudinary.UploadAsync(uploadParams);
var url = result.SecureUrl.ToString();
urlList.Add(url);
}
return urlList;
}
我没有收到异常,但来自 Cloudinary 的结果消息有一条错误消息:"No image";
调试时我看到这些错误:
我需要在此代码中修复什么?
最有可能的是,假设其他一切正常,您只需要在 MemoryStream
:
ms.Position = 0;
如此完整的示例:
public async Task<List<string>> GetImagesUrlsByImage(IFormFile image)
{
List<string> urlList = new List<string>();
ImageUploadParams uploadParams = new ImageUploadParams();
using (var memoryStream = new MemoryStream())
{
await image.CopyToAsync(memoryStream);
ms.Position = 0; // set cursor to the beginning of the stream.
uploadParams.File = new FileDescription(image.FileName, memoryStream);
uploadParams.EagerTransforms = new List<Transformation>
{
new EagerTransformation().Width(200).Height(150).Crop("scale"),
new EagerTransformation().Width(500).Height(200).Crop("scale")
};
ImageUploadResult result = await _cloudinary.UploadAsync(uploadParams);
var url = result.SecureUrl.ToString();
urlList.Add(url);
}
return urlList;
}