C# 将已发布的图像转发到WCF Rest服务
我有一个用C#编写的webform应用程序,我想做的是在用户提交一个带有图像的HTML表单后,我将该图像发送到同样用C#编写的WCF Rest服务。 问题是,当我在web服务中获取映像时,它已损坏 我想问题是我没有正确地对文件进行编码,但在互联网上阅读了几天之后,我还没有找到任何线索 网络表单代码:C# 将已发布的图像转发到WCF Rest服务,c#,wcf,httpwebrequest,C#,Wcf,Httpwebrequest,我有一个用C#编写的webform应用程序,我想做的是在用户提交一个带有图像的HTML表单后,我将该图像发送到同样用C#编写的WCF Rest服务。 问题是,当我在web服务中获取映像时,它已损坏 我想问题是我没有正确地对文件进行编码,但在互联网上阅读了几天之后,我还没有找到任何线索 网络表单代码: protected void Page_Load(object sender, EventArgs e) { HttpPostedFile image = Request.Files["i
protected void Page_Load(object sender, EventArgs e)
{
HttpPostedFile image = Request.Files["imagen"];
string serverResponse = Send("mywebservice/postimage", "POST", Encoding.UTF8.GetBytes(StreamToString(image.InputStream)));
}
[WebInvoke(UriTemplate = "upload-user-image", Method = "POST")]
public Stream UploadUserImage(Stream streamdata)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(streamImagen, true);
// here I get a format error
}
编辑(这种方式有效)
网络服务代码:
protected void Page_Load(object sender, EventArgs e)
{
HttpPostedFile image = Request.Files["imagen"];
string serverResponse = Send("mywebservice/postimage", "POST", Encoding.UTF8.GetBytes(StreamToString(image.InputStream)));
}
[WebInvoke(UriTemplate = "upload-user-image", Method = "POST")]
public Stream UploadUserImage(Stream streamdata)
{
System.Drawing.Image img = System.Drawing.Image.FromStream(streamImagen, true);
// here I get a format error
}
可能有一个问题:您正在将字节流转换为字符串,然后再转换回字节流。图像字节是任意字节,可能映射到字符串,也可能不映射到字符串,并且在一次转换中使用的编码(encoding.Default)可能与在另一次转换中使用的编码(encoding.UTF8)不同 而不是这样做:
Encoding.UTF8.GetBytes(StreamToString(image.InputStream))
试着做类似的事情
MemoryStream ms = new MemoryStream();
image.InputStream.CopyTo(ms);
byte[] bytes = ms.ToArray();
或者只需将流传递到
Send
并将其复制到请求流。可能有一个问题:您正在将字节流转换为字符串,然后再转换回字节流。图像字节是任意字节,可能映射到字符串,也可能不映射到字符串,并且在一次转换中使用的编码(encoding.Default)可能与在另一次转换中使用的编码(encoding.UTF8)不同
而不是这样做:
Encoding.UTF8.GetBytes(StreamToString(image.InputStream))
试着做类似的事情
MemoryStream ms = new MemoryStream();
image.InputStream.CopyTo(ms);
byte[] bytes = ms.ToArray();
或者只需将流传递到
Send
并将其复制到请求流。您能提供错误消息吗?我用西班牙语收到,但上面写着“不正确的参数/参数”您能提供错误消息吗?我用西班牙语收到,但上面写着“不正确的参数/参数”谢谢卡洛斯,成功了!你的另一个选择似乎也是个好主意!谢谢你,卡洛斯,成功了!你的另一个选择似乎也是个好主意!