Skip to main content
se añadieron 6303 caracteres en el cuerpo
Origen Enlace
    public byte[] File { get; set; }
    public string FileName { get; set; }
    public string ContentType { get; set; }

    public FormUpload(byte[] file, string filename, string contenttype)
    {
        File = file;
        FileName = filename;
        ContentType = contenttype;
    }
    private static readonly Encoding encoding = Encoding.UTF8;

    //primer metodo
    public static HttpWebResponse MultipartFormPost(string postUrl, string userAgent, Dictionary<string, object> postParameters, string headerkey, string headervalue)
    {
        WebClient wc = new WebClient();
        //var fileName = "C:\\Users\\usuario\\AppData\\Local\\Temp\\Snapshot_81689ad276524d5.jpg";
        var fileName = "D:\\SEGESCOT\\Sintítulo.png";
       // var fileName = "https://cflvdg.avoz.es/sc/uo6Kh0onO3js1onHHNrRLV7X-Os=/x/2019/02/26/00121551208683005769902/Foto/HF27C11F1_201630.jpg";
        postUrl = "https://api.trello.com/1/cards";
        string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
        string contentType = "multipart/form-data; boundary=" + formDataBoundary;
        postParameters.Add("name", "prueba");
        postParameters.Add("desc", "prueba");
        postParameters.Add("idList", "prueba");
        postParameters.Add("keepFromSource", "all");
        postParameters.Add("key", "prueba");
        postParameters.Add("token", "prueba");
        byte[] bytes = wc.DownloadData(fileName); // You need to do this download if your file is on any other server otherwise you can convert that file directly to bytes 
        postParameters.Add("fileSource", new FormUpload(bytes, Path.GetFileName(fileName), "image/png"));
        byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);

        return PostForm(postUrl, userAgent, contentType, formData, headerkey, headervalue);
    }

    //Armar request
    private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
    {
        Stream formDataStream = new System.IO.MemoryStream();
        bool needsCLRF = false;

        foreach (var param in postParameters)
        {

            if (needsCLRF)
                formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));

            needsCLRF = true;

            if (param.Value is FormUpload) // to check if parameter if of file type   
            {
                FormUpload fileToUpload = (FormUpload)param.Value;

                // Add just the first part of this param, since we will write the file data directly to the Stream  
                string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                    boundary,
                    param.Key,
                    fileToUpload.FileName ?? param.Key,
                    fileToUpload.ContentType ?? "application/octet-stream");

                formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));

                // Write the file data directly to the Stream, rather than serializing it to a string.  
                formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
            }
            else
            {
                string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
                    boundary,
                    param.Key,
                    param.Value);
                formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
            }
        }

        // Add the end of the request.  Start with a newline  
        string footer = "\r\n--" + boundary + "--\r\n";
        formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));

        // Dump the Stream into a byte[]  
        formDataStream.Position = 0;
        byte[] formData = new byte[formDataStream.Length];
        formDataStream.Read(formData, 0, formData.Length);
        formDataStream.Close();

        return formData;
    }

    //Hacer el request
    private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData, string headerkey, string headervalue)
    {
        HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;

        if (request == null)
        {
            throw new NullReferenceException("request is not a http request");
        }

        // Set up the request properties.  
        request.Method = "POST";
        request.ContentType = contentType;
        request.UserAgent = userAgent;
        request.CookieContainer = new CookieContainer();
        request.ContentLength = formData.Length;

        // You could add authentication here as well if needed:  
        // request.PreAuthenticate = true;  
        // request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;  

        //Add header if needed  
        request.Headers.Add("key", "13b23b1c614accbf488a4540f4e87b33");

        // Send the form data to the request.  
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(formData, 0, formData.Length);
            requestStream.Close();
        }

        return request.GetResponse() as HttpWebResponse;
    }
}

En este caso le entra un array de imagenes pero no sé como recorrer e insertar las imagenes una por una, alguien que me ayude por favor

En este caso le entra un array de imagenes pero no sé como recorrer e insertar las imagenes una por una, alguien que me ayude por favor

En este caso le entra un array de imagenes pero no sé como recorrer e insertar las imagenes una por una, alguien que me ayude por favor

    public byte[] File { get; set; }
    public string FileName { get; set; }
    public string ContentType { get; set; }

    public FormUpload(byte[] file, string filename, string contenttype)
    {
        File = file;
        FileName = filename;
        ContentType = contenttype;
    }
    private static readonly Encoding encoding = Encoding.UTF8;

    //primer metodo
    public static HttpWebResponse MultipartFormPost(string postUrl, string userAgent, Dictionary<string, object> postParameters, string headerkey, string headervalue)
    {
        WebClient wc = new WebClient();
        //var fileName = "C:\\Users\\usuario\\AppData\\Local\\Temp\\Snapshot_81689ad276524d5.jpg";
        var fileName = "D:\\SEGESCOT\\Sintítulo.png";
       // var fileName = "https://cflvdg.avoz.es/sc/uo6Kh0onO3js1onHHNrRLV7X-Os=/x/2019/02/26/00121551208683005769902/Foto/HF27C11F1_201630.jpg";
        postUrl = "https://api.trello.com/1/cards";
        string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
        string contentType = "multipart/form-data; boundary=" + formDataBoundary;
        postParameters.Add("name", "prueba");
        postParameters.Add("desc", "prueba");
        postParameters.Add("idList", "prueba");
        postParameters.Add("keepFromSource", "all");
        postParameters.Add("key", "prueba");
        postParameters.Add("token", "prueba");
        byte[] bytes = wc.DownloadData(fileName); // You need to do this download if your file is on any other server otherwise you can convert that file directly to bytes 
        postParameters.Add("fileSource", new FormUpload(bytes, Path.GetFileName(fileName), "image/png"));
        byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);

        return PostForm(postUrl, userAgent, contentType, formData, headerkey, headervalue);
    }

    //Armar request
    private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
    {
        Stream formDataStream = new System.IO.MemoryStream();
        bool needsCLRF = false;

        foreach (var param in postParameters)
        {

            if (needsCLRF)
                formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));

            needsCLRF = true;

            if (param.Value is FormUpload) // to check if parameter if of file type   
            {
                FormUpload fileToUpload = (FormUpload)param.Value;

                // Add just the first part of this param, since we will write the file data directly to the Stream  
                string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                    boundary,
                    param.Key,
                    fileToUpload.FileName ?? param.Key,
                    fileToUpload.ContentType ?? "application/octet-stream");

                formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));

                // Write the file data directly to the Stream, rather than serializing it to a string.  
                formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
            }
            else
            {
                string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
                    boundary,
                    param.Key,
                    param.Value);
                formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
            }
        }

        // Add the end of the request.  Start with a newline  
        string footer = "\r\n--" + boundary + "--\r\n";
        formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));

        // Dump the Stream into a byte[]  
        formDataStream.Position = 0;
        byte[] formData = new byte[formDataStream.Length];
        formDataStream.Read(formData, 0, formData.Length);
        formDataStream.Close();

        return formData;
    }

    //Hacer el request
    private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData, string headerkey, string headervalue)
    {
        HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;

        if (request == null)
        {
            throw new NullReferenceException("request is not a http request");
        }

        // Set up the request properties.  
        request.Method = "POST";
        request.ContentType = contentType;
        request.UserAgent = userAgent;
        request.CookieContainer = new CookieContainer();
        request.ContentLength = formData.Length;

        // You could add authentication here as well if needed:  
        // request.PreAuthenticate = true;  
        // request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;  

        //Add header if needed  
        request.Headers.Add("key", "13b23b1c614accbf488a4540f4e87b33");

        // Send the form data to the request.  
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(formData, 0, formData.Length);
            requestStream.Close();
        }

        return request.GetResponse() as HttpWebResponse;
    }
}

En este caso le entra un array de imagenes pero no sé como recorrer e insertar las imagenes una por una, alguien que me ayude por favor

En este caso le entra un array de imagenes pero no sé como recorrer e insertar las imagenes una por una, alguien que me ayude por favor

En este caso le entra un array de imagenes pero no sé como recorrer e insertar las imagenes una por una, alguien que me ayude por favor

Origen Enlace

Enviar más de una imagen por post

Tengo un código para crear tarjetas de trello por medio de c#, puedo crear la tarjeta y enviarle una imagen, pero no sé cómo enviar más de una imagen para una misma tarjeta, alguien que me pueda dar una guía?