[英]Put multiple Attachments to CouchDB
I have an .NET Core console application which does some update stuff to my CouchDB.我有一个 .NET Core 控制台应用程序,它对我的 CouchDB 做了一些更新。 Now I have to add multiple attachments (in this case images) to my doc.
现在我必须向我的文档添加多个附件(在本例中为图像)。
Here ( https://docs.couchdb.org/en/stable/api/document/common.html#attachments ) in point 1.4.1.1.4 they describe the way, but I get a Bad Request, I think my json is not perfect.在这里( https://docs.couchdb.org/en/stable/api/document/common.html#attachments )在第 1.4.1.1.4 点他们描述了这种方式,但我收到了一个错误的请求,我认为我的 json 是不完美。 So I have a list of images that I convert and then send to DB, here is my code:
所以我有一个我转换然后发送到数据库的图像列表,这是我的代码:
List<ImageFromApi> imagesFromApi = new List<ImageFromApi>();
string base64String;
foreach (var image in Images)
{
using (image)
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
base64String = Convert.ToBase64String(imageBytes);
}
}
ImageFromApi Imagebuffer = new ImageFromApi() {
content_type = "image/*",
data = base64String
};
imagesFromApi.Add(Imagebuffer);
DocAttachments bufferData = new DocAttachments() {imagesFromApi = imagesFromApi };
newImages._attachments = bufferData;
}
string imagesJson = JsonConvert.SerializeObject(newImages);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(imagesJson);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
My Json from this code looks like this:这段代码中我的 Json 如下所示:
I see the difference between mine and the one from the documentation, but I dont know how to change my json correctly.我从文档中看到了我的和那个之间的区别,但我不知道如何正确更改我的 json。
Any solutions?有什么解决办法吗?
So that nobody had a solution, i tried something around and find a way to achieve what i need.所以没有人有解决方案,我尝试了一些方法并找到了实现我需要的方法。 I made a new Attachment and added each image manually.
我制作了一个新附件并手动添加了每个图像。 Here is an example of the first image:
这是第一张图片的示例:
Attachments attBuffer = new Attachments();
using (imagesFromApi[0])
{
using (MemoryStream m = new MemoryStream())
{
partImgs[0].Save(m, partImgs[0].RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
base64String = Convert.ToBase64String(imageBytes);
}
}
attBuffer.Hood = new Hood()
{
content_type = "image/jpeg",
data = base64String,
};
As you can see, i created any of my images as a class and then mapped the list to it.如您所见,我将我的任何图像创建为 class,然后将列表映射到它。 I think this is not the best way, but it works for me, so better than nothing.
我认为这不是最好的方法,但它对我有用,总比没有好。
Hope it helps you as well.希望它也能帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.