问题描述
首先,我是.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)
时会抛出
所以可能是因为它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);