C#.net Windows窗体;通过图片框上传包含Cloudinary的图片

问题描述

首先,我是.net / C#的新手

我希望用户通过图片框选择图片,并且该图片应直接上传到cloudinary

public static Cloudinary cloudinary;

public const string CLOUD_NAME = "XXXXXX";
public const string API_KEY = "XXXXXXX";
public const string API_SECRET = "XXXXXX";

string imagePath = "";
static void cloudinaryAccount(string[] args)
{
    Account account = new Account(CLOUD_NAME,API_KEY,API_SECRET);
    cloudinary = new Cloudinary(account);
}

private void pictureBox1_Click(object sender,EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "All files(*.*)|*.*";
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        imagePath = dialog.FileName.ToString();
        pictureBox1.ImageLocation = imagePath;
    }
    uploadImage(imagePath); // pass the path of the image to the uploadImage() function.
}

public static void uploadImage(string imagePath)
{

    var UploadParams = new ImageUploadParams()
    {
        File = new FileDescription(imagePath)
    };


    var uploadResult = cloudinary.Upload(UploadParams);
}

但是当涉及到cloudinary.Upload(UploadParams)时会抛出

this error

所以可能是因为它cloudinary为空。 所以如果我搬走

Account account = new Account(CLOUD_NAME,API_SECRET);
cloudinary = new Cloudinary(account);

进入uploadImage(string imagePath)函数,出现以下错误:

System.IO.FileNotFoundException:'无法加载文件或程序集'Newtonsoft.Json,版本= 12.0.0.0,Culture =中性,PublicKeyToken = 30ad4fe6b2a6aeed'或其依赖项之一。系统找不到指定的文件。'

我按照here的步骤进行了修复,现在可以正常工作了。

解决方法

确保在调用upload之前运行以下行:

cloudinary = new Cloudinary(account);

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...