![](/img/trans.png)
[英]Web Api 2 not returning ByteArrayContent to HTTPClient
[英]get bytearraycontent in web service
我正在尝试将图像解析为字节数组并将其发送到我的Web服务。 问题是,我找不到任何读取bytearraycontent的方法(过去我使用过HttpContext.Current.Request.Files,但显然它不存在)...有什么帮助吗?
编辑-我设法获取了添加的表单数据,但无法正确保存图像。 我切换到stringContent,它仍然不起作用,接收到的字符串与我发送的字符串大小完全相同,但是无法打开它。 在web.config中添加了'requestValidationMode =“ 2.0”'。
码:
public async Task uploadAP()
{
using (var client = new HttpClient())
{
MultipartFormDataContent form = new MultipartFormDataContent();
string str = File.ReadAllText(DEBRIS_PIC_PATH);
form.Add(new StringContent(str), "ap");
HttpResponseMessage response = await client.PostAsync("http://192.168.1.10:8080/WS.asmx/uploadAP", form);
}
}
显然是这样的:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void uploadAP()
{
string t = HttpContext.Current.Request.Form["ap"];
FileStream objfilestream = new FileStream(debrisApPath, FileMode.Create, FileAccess.ReadWrite);
objfilestream.Write(binaryWriteArray, 0, binaryWriteArray.Length);
objfilestream.Close();
}
对于延迟,我们深表歉意。 这是我向旧式ASMX Web服务承诺的一个示例,该示例将从客户端读取ByteArrayContent
,之后我将提供两个警告...
using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Collections.Specialized;
using System.ServiceModel.Activation;
namespace OldWSTest
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string uploadAP()
{
var foo = HttpContext.Current.Request.Form["ap"];
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(foo);
// do whatever you need with the bytes here
return "done";
}
}
}
我肯定会回应约翰·桑德斯(John Saunders)的评论,即对于基础的Web服务工作,这样的项目应该对WCF / WebAPI而不是ASMX进行很好的检查。 我忘记了基于ASMX的Web服务可能会带来的痛苦。
我不会保证这是在Web服务端获取此数据的理想方法。 几乎肯定会有更优雅/有效/更好/更流畅/更快的方法。 我一直在寻找与旧式Web服务模型的局限性相关的障碍。 但是,据我所能测试的,这是可行的”
AspNetCompatibilityRequirements
模式允许我访问Form集合,而如果没有它,那么在没有解析/钻取边界数据的情况下根本无法使用它。
祝好运。 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.