【问题标题】:Empty File error while uploading images to Cloudianary using ASP.NET Core web API使用 ASP.NET Core Web API 将图像上传到 Cloudianary 时出现空文件错误
【发布时间】:2021-09-05 01:37:12
【问题描述】:

我在使用 ASP.NET Core Web API 将图像上传到 Cloudinary 时遇到问题。 上传到 Cloudinary 的方法使用 IFormFile 接收图像,然后将其复制到流中。然后我的 Cloudinary 上传方法被配置为将图像上传到 Cloudinary,但是当调用上传方法时我得到一个错误,空文件。 这是我的代码。

public class CloudinaryAPI
{
    private readonly Cloudinary cloudinary;

    /// <summary>
    /// Set up cloudinary account 
    /// </summary>
    /// <param name="cloud">Cloud name</param>
    /// <param name="apiKey">API Key</param>
    /// <param name="apiSecret">API Secret</param>
    public CloudinaryAPI(string cloud = "", 
        string apiKey = "", string apiSecret = "")
    {
        Account account = new Account
        {
            Cloud = cloud,
            ApiKey = apiKey,
            ApiSecret = apiSecret
        };

        this.cloudinary = new Cloudinary(account);
    }

    /// <summary>
    /// Upload image using a Stream
    /// </summary>
    /// <param name="tream"></param>
    /// <returns></returns>
    public async Task<ImageUploadResult> UploadImage(Stream stream)
    {
        if(stream != null)
        {
            var uploadParams = new ImageUploadParams()
            {
                File = new FileDescription(Guid.NewGuid().ToString(), stream)
            };
            var uploadResult = await cloudinary.UploadAsync(uploadParams);
            return uploadResult;
        }
        throw new ArgumentNullException(message: "Something went wrong while trying to process image", null);
    }
}

这是接收请求的控制器动作。

    public async Task<IActionResult> UpdateProfilePicture(IFormFile 
    fileUpload)
    {
        using (Stream stream = new MemoryStream())
        {
            stream.Flush();
            stream.Position = 0;
            await fileUpload.CopyToAsync(stream);

            // Upload the file if less than 500KB

            if (stream.Length < 524288)
            {
                // Upload image to cloudinary
                await cloudinary.UploadImage(stream);
            }

【问题讨论】:

    标签: image asp.net-core asp.net-core-webapi cloudinary


    【解决方案1】:

    这是常见错误。您需要从FromForm 获取文件。 所以你需要通过表单将文件发送到后端。

     public async Task<IActionResult> UpdateProfilePicture([FromForm] IFormFile fileUpload)
     { ... }
    

    【讨论】:

    • 好的,但是我正在 Swagger 上进行测试,所以使用 FromForm,我该如何在其中添加我的文件详细信息。
    • 调试时我发现 IFormFile 从上传的图像中接收到所有信息,但是在尝试将图像复制到内存流时出现 readtimeout 和 writetimeout 异常。
    • 所以你确定fileUpload 不为空?顺便说一句,你应该检查一下。
    【解决方案2】:

    我发现我必须在从 IFormFile 复制流后重置流的位置。

    public async Task<IActionResult> UpdateProfilePicture(IFormFile 
    fileUpload)
    {
        using (Stream stream = new MemoryStream())
        {
            stream.Flush();
            await fileUpload.CopyToAsync(stream);
            stream.Position = 0;
    

    在将文件复制到流之后应该将 stream.Position 设置为 0。

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 2017-10-21
      • 2019-08-07
      • 2017-02-24
      • 1970-01-01
      相关资源
      最近更新 更多