我上传图片,并希望将其发送到第三方服务(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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句