Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8220627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:35:33+00:00 2026-06-07T13:35:33+00:00

I have a console application that encodes an image as base64String and then sends

  • 0

I have a console application that encodes an image as base64String and then sends it to a server as part of an http POST request.

Problem is, when I open it up to examine it server side, the data is no longer there.

Here is the code I’m using to send the string-

HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(ip);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
string formPostString = "image=" + HttpUtility.UrlEncode(imageBase64String);
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(formPostString);
webRequest.ContentLength = buffer.Length;
using (Stream webStream = webRequest.GetRequestStream())
{
    webStream.Write(buffer, 0, buffer.Length);
}

And the code I have handling it serverside-

private void OnRequest(object source, RequestEventArgs args)
{
    IHttpClientContext context = (IHttpClientContext)source;
    IHttpRequest request = args.Request;

    ImageConverter imgConvert = new ImageConverter();
    string imageBase64String = request.Form["image"].Value;
    Image newImg = imgConvert.Base64StringToImage(imageBase64String);
    newImg.Save(@"..\Images\test.png", System.Drawing.Imaging.ImageFormat.Png);
}

When it reaches the point of making the new Image object, the imageBase64String is null. I can’t seem to identify why this is happening.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:35:35+00:00Added an answer on June 7, 2026 at 1:35 pm

    The client code you have shown never sends a request. You must call webRequest.GetResponse() for the request to be sent. But I would simplify this client code suing a WebClient:

    using (var client = new WebClient())
    {
        var data = new NameValueCollection
        {
            { "image", imageBase64String }
        };
        var result = client.UploadValues(ip, data);
        Console.WriteLine(Encoding.Default.GetString(result));
    }
    

    As far as your server side code is concerned you seem to be using some third party framework which you haven’t mentioned in your question. For example the following ASP.NET handler works perfectly fine (~/UploadImage.ashx) with the client shown previously:

    public class UploadImage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var imageBase64String = context.Request.Form["image"];
            var imageBuffer = Convert.FromBase64String(imageBase64String);
            using (var stream = new MemoryStream(imageBuffer))
            using (var image = Image.FromStream(stream))
            {
                image.Save(@"c:\work\test.png");
            }
    
            context.Response.ContentType = "text/plain";
            context.Response.Write("upload successful");
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    }
    

    But if we are going to upload files to a server, why reinventing wheels? Why using base64 when there’s application/x-www-form-urlencoded with a wheel reinvented Base64 encoding which hugely increases the request size when there’s multipart/form-data?

    So let’s improve our client to take advantage of this specifically designed for the purpose encoding. Let’s start by writing an extension method for the WebClient which will allow us to directly upload one or more files:

    public class UploadFile
    {
        public UploadFile()
        {
            ContentType = "application/octet-stream";
        }
        public string Name { get; set; }
        public string Filename { get; set; }
        public string ContentType { get; set; }
        public Stream Stream { get; set; }
    }
    
    public static class WebClientExtensions
    {
        public static byte[] UploadFiles(this WebClient client, string address, IEnumerable<UploadFile> files, NameValueCollection values)
        {
            var request = WebRequest.Create(address);
            request.Method = "POST";
            var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            boundary = "--" + boundary;
    
            using (var requestStream = request.GetRequestStream())
            {
                // Write the values
                foreach (string name in values.Keys)
                {
                    var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                    requestStream.Write(buffer, 0, buffer.Length);
                    buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
                    requestStream.Write(buffer, 0, buffer.Length);
                    buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
                    requestStream.Write(buffer, 0, buffer.Length);
                }
    
                // Write the files
                foreach (var file in files)
                {
                    var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                    requestStream.Write(buffer, 0, buffer.Length);
                    buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
                    requestStream.Write(buffer, 0, buffer.Length);
                    buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
                    requestStream.Write(buffer, 0, buffer.Length);
                    file.Stream.CopyTo(requestStream);
                    buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
                    requestStream.Write(buffer, 0, buffer.Length);
                }
    
                var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
                requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
            }
    
            using (var response = request.GetResponse())
            using (var responseStream = response.GetResponseStream())
            using (var stream = new MemoryStream())
            {
                responseStream.CopyTo(stream);
                return stream.ToArray();
            }
        }
    }
    

    and then the client will look like this:

    byte[] image = ... go and fetch the image that you want to upload
    
    using (var stream = new MemoryStream(image))
    using (var client = new WebClient())
    {
        var files = new[]
        {
            new UploadFile
            {
                Name = "image",
                Filename = "test.jpg",
                ContentType = "image/jpg",
                Stream = stream
            }
        };
        var result = client.UploadFiles("http://localhost:6830/uploadimage.ashx", files, new NameValueCollection());
        Console.WriteLine(Encoding.Default.GetString(result));
    }
    

    and the server we handler we have seen earlier:

    public class UploadImage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var uploadedImage = context.Request.Files["image"];
            using (var image = Image.FromStream(uploadedImage.InputStream))
            {
                image.Save(@"c:\work\test.png");
            }
    
            context.Response.ContentType = "text/plain";
            context.Response.Write("upload successful");
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    }
    

    Guess, what the next step in improvement of this code is? Answer: an asynchronous client request of course that will take advantage of I/O Completion Ports instead of blocking the calling thread while waiting for the file to be uploaded to the server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a console application that sends a XML to a MVC application and
I am building a console application that gathers some data and then sends it
I have a console application that is server based. I only want 1 instance
I have a console application that sends customized emails (with attachments) to different recipients
I have a console application that successfully re-sizes an image while maintaining aspect ratio.
Can anyone help? I have a PHP method that sends an http post: <?php
I have a console application that uses a number of command line switches to
I have a console application that should do best effort logging to a database
I have a console application that communicates with a web service. Both of them
I have a console application that is trying to load a CustomConfigurationSection from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.