使用POST发送图像 [英] Send Image using POST

查看:142
本文介绍了使用POST发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序和一个ASP.NET MVC站点。 WPF应用程序使用Kinect捕获图像,这些图像保存为文件。我想要做的是将文件从WPF应用程序发送到ASP.NET MVC站点。

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.

我尝试了以下从图像文件中获取字节和使用Base64将其转换为字符串,然后在另一端尝试将字符串转换回字节,然后再转换回文件。整个过程都有效,除了最后的文件已损坏且无法加载。

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应用

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网站

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


推荐答案

你正在做一些奇怪的事情与编码。如果您将文件名作为标题传递,可能会更好。您可以通过使用HttpContext.Current.Request在MVC端获取文件名。然后,只需将您在wpf应用程序中编写的RequestStream更改为:

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

这篇关于使用POST发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆