PriestVallon
PriestVallon

Reputation: 1518

Send Image using POST

I have a WPF application and a ASP.NET MVC site. The WPF application uses the Kinect to capture images and these are saved as files. What I want to do is send the files from the WPF application to the ASP.NET MVC site.

I have tried the following which gets the bytes from the image file and converts it to a string using Base64 and then on the other side try to convert the string back to bytes and then back to a file. The whole process works except the files at the end are corrupt and won't load.

Also is the correct way of sending files or would I be better off trying to use Sockets?

WPF Application

var imageUrl = "http://127.0.0.1:18710/Home/Index";

//byte[] imageBytes = set.getImageBytes();
byte[] imb = System.Text.Encoding.UTF8.GetBytes("imagename=" + ImageName + ".png&image=" + Convert.ToBase64String(File.ReadAllBytes(ImageName + ".png")));

var imageReq = (HttpWebRequest)WebRequest.Create(imageUrl);

imageReq.Method = "POST";
imageReq.ContentType = "application/x-www-form-urlencoded";
imageReq.ContentLength = imb.Length;


using (Stream os = imageReq.GetRequestStream())
{
    os.Write(imb, 0, imb.Length);
}

ASP.NET MVC Site

if (image != null && imagename != null)
{
    System.IO.File.WriteAllBytes(@"c:\" + imagename, Convert.FromBase64String(image));
}

Upvotes: 2

Views: 2597

Answers (1)

Slack Shot
Slack Shot

Reputation: 1110

You are doing some weird stuff with encoding. It's probably better if you pass the file name in as a header.. You can fetch the filename out on the MVC side.. by using HttpContext.Current.Request. Then, just change your RequestStream you are writing in your wpf app, to this:

byte[] imb = File.ReadAllBytes(ImageName + ".png")));

Upvotes: 3

Related Questions