1

I'm trying to send an image to trello rest api, and pass mimetype and filename along using WebClient.

In python I can do this with the following function:

resp = requests.post("https://trello.com/1/cards/{}/attachments".format(trelloCardID),
                     params={'key': key, 'token': token},
                     files={'file': (theName, imgdata, theMIMEtype)})

Where theName is a filename, imgdata is the file an theMIMEtype is the mimetype.

In c# I get close with WebClient but I can't figure out how to pass the mimetype in the same way.

I'm passing this parameter along with the file, but here name and mimetype has separate entries.

myNameValueCollection.Add("token", token);
myNameValueCollection.Add("key", key);
myNameValueCollection.Add("name", "testName.png");
myNameValueCollection.Add("mimetype", "image/png");
myNameValueCollection.Add("file", base64Decoded);

I could create a separate NameValueCollection for the filename, file and mimetype, but NameValueCollection only accepts key,value pairs.

Any help is very much appreciated.

For now trello api doesn't recognize the filetype.

This is my c# code:

using Newtonsoft.Json;
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;

namespace RescoTrello
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();

            //Define Token, key and url
            string key = "xxxx";
            string token = "yyyy";

            var url = string.Format("https://api.trello.com/1/cards/{0}/attachments", "zzzz");

            // Get file containing base64 encoded string from filesystem and convert it to a file
            string fileName = "C:/file.txt";
            string base64Encoded = System.IO.File.ReadAllText(fileName);

            byte[] data = System.Convert.FromBase64String(base64Encoded);
            string base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(data);

            //Build NameValueCollection to send along with the request
            NameValueCollection myNameValueCollection = new NameValueCollection();

            myNameValueCollection.Add("token", token);
            myNameValueCollection.Add("key", key);
            myNameValueCollection.Add("name", "testName.png");
            myNameValueCollection.Add("mimetype", "image/png");
            myNameValueCollection.Add("file", base64Decoded);

            //Send the request
            try
            {
                var result = myWebClient.UploadValues(url, "POST", myNameValueCollection);

                //Convert result to json and further to object model
                Rootobject resultJson = JsonConvert.DeserializeObject<Rootobject>(Encoding.ASCII.GetString(result));
                Console.WriteLine(resultJson.id);
            }
            catch (WebException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
Kresten
  • 810
  • 13
  • 36
  • [seems related](https://stackoverflow.com/a/11048296/4033690) – momo Nov 06 '21 at 20:44
  • It is, but what would you do if the file is not comming from the filesystem but from a string? I would like to do something like using (var stream = base64Decoded) //File.Open("C:/file.jpg", FileMode.Open)) but it won't pass to the function. – Kresten Nov 06 '21 at 22:05
  • create a Stream, e.g., `new MemoryStream(Encoding.UTF8.GetBytes())` – momo Nov 07 '21 at 07:43
  • No luck using string base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(data); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(base64Decoded))) – Kresten Nov 07 '21 at 13:52
  • I don't get a file trough – Kresten Nov 07 '21 at 13:53

0 Answers0