简体   繁体   中英

Put multiple Attachments to CouchDB

I have an .NET Core console application which does some update stuff to my 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. 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: 迈杰森

I see the difference between mine and the one from the documentation, but I dont know how to change my json correctly.

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. I think this is not the best way, but it works for me, so better than nothing.

Hope it helps you as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM