You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cloudinary C#上传报错:UploadParams中必须指定文件名

解决Cloudinary .NET上传报错“必须在UploadParams中指定文件名!”

问题根源

你传入FileDescription的是文件夹路径(比如wwwroot/images/userId/),而非具体的图片文件完整路径,Cloudinary无法定位到要上传的目标文件,因此抛出该错误。

修复步骤

  1. 拼接完整文件路径
    将imageDictionary中的文件夹路径与文件名拼接,生成完整的文件路径后传给FileDescription:
    var fullImagePath = Path.Combine(imageDictionary[Consts.IMAGE_PATH], imageDictionary[Consts.IMAGE_NAME]);
    var uploadParams = new ImageUploadParams
    {
        File = new FileDescription(fullImagePath),
        UniqueFilename = false,
        UseFilename = true,
        PublicId = GetCloudinaryPublicId(imageDictionary[Consts.IMAGE_NAME]) 
    };
    
  2. 提前验证文件存在性
    上传前增加文件存在判断,避免因文件缺失引发额外异常:
    var fullImagePath = Path.Combine(imageDictionary[Consts.IMAGE_PATH], imageDictionary[Consts.IMAGE_NAME]);
    if (!File.Exists(fullImagePath))
    {
        return new ErrorResult("指定的图片文件不存在");
    }
    
  3. 优化PublicId格式(可选)
    Cloudinary的PublicId不需要包含文件后缀,若当前GetCloudinaryPublicId返回的是带后缀的路径(比如images/userId/123123.jpg),建议移除后缀,简化资源标识:
    var fileNameWithoutExt = Path.GetFileNameWithoutExtension(imageDictionary[Consts.IMAGE_NAME]);
    PublicId = $"images/userId/{fileNameWithoutExt}"
    

内容的提问来源于stack exchange,提问作者Muhammet İkbal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 22:35:55