2

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));
}
PriestVallon
  • 1,519
  • 1
  • 22
  • 44

1 Answers1

3

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")));

Slack Shot
  • 1,090
  • 6
  • 10
  • How am I going to reassemble the file on the MVC side? When I look at Request.Form it has 1037 different keys. – PriestVallon Aug 30 '13 at 00:14
  • Change your Content-Type to "image/png". Or application/octet-stream and make the parameter incoming to your index page.. "Stream myimagestream", from there you should be able to save the stream, wherever you want. – Slack Shot Aug 30 '13 at 00:17
  • A stream.. is just a blob of "stuff". You shouldn't be afraid of them.. -smiles- Once you realize that.. and that most file types can be uniquely recognized by the first 256 bytes of the stream.. you'll be a happier man. (Zip files are an exception to the 256 byte rule.. because a lot of files are really zip files.. modern powerpoint files.. jar files.. modern excel files.. ) – Slack Shot Aug 30 '13 at 00:21
  • That still returns the same thing in Request.Form. – PriestVallon Aug 30 '13 at 00:24
  • You don't need Request.Form. If you modified your "Index" Post method to take a parameter of a Stream myimagestream like this... Index(Stream myimagestream) You'll be able to just use the variable. – Slack Shot Aug 30 '13 at 00:27
  • Or you can use this : System.IO.StreamReader reader = new System.IO.StreamReader(HttpContext.Current.Request.InputStream); – Slack Shot Aug 30 '13 at 00:28
  • The 2nd one would be easier for you.. Man, you are making me work for this "Accepted" answer. : ) – Slack Shot Aug 30 '13 at 00:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36497/discussion-between-slack-shot-and-priestvallon) – Slack Shot Aug 30 '13 at 00:42