我正在尝试将图像解析为字节数组并将其发送到我的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();
}
答案 0 :(得分:1)
对延迟道歉。这是我用旧式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&#39;评论说,对于基础Web服务工作,这样的项目应该好好,严格地看看WCF / WebAPI,而不是ASMX。我忘记了基于ASMX的Web服务可能带来的痛苦。
承诺这是在网络服务方面获取此数据的理想方式;几乎肯定会有更优雅/更有效/更好/更光滑/更快速的方式来实现它。我一直在寻找我认为与旧式Web服务模型的局限性相关的障碍。然而,这是我可以测试的最好的工作&#34;
AspNetCompatibilityRequirements
模式允许我访问Form集合,而没有它,在没有解析/钻取边界数据的情况下它根本不可用。