简体   繁体   English

将图像作为字节 [] 作为 Web 服务方法参数发送

[英]Sending Image as a byte[] as a web service method arguments

im facing a problem in which when i send a small size byte array我面临一个问题,当我发送一个小字节数组时

byte[] s = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

then it will received and inserted in database successfully but when it comes to a bytes array of image converted to a bytes[] then i will show an exception然后它将成功接收并插入到数据库中,但是当涉及到转换为字节 [] 的图像字节数组时,我将显示异常

"The request failed with HTTP status 404: Not Found." “请求失败,HTTP 状态为 404:未找到。”

it does not show that "maximum limit exceed" or something like that.它没有显示“超过最大限制”或类似的东西。 what should i do?我该怎么办? here is the screen shot这是屏幕截图

在此处输入图片说明

it send the byte array that is commented but the array in arguments is the bitmap converted to byte array.它发送被注释的字节数组,但参数中的数组是转换为字节数组的位图。

- Edited - 编辑

Here is the code of receiving end in web service这是Web服务中接收端的代码

 [WebMethod]
 public bool TakeScreenShotResponseBack(string ip, byte[] screenShot)
    {
        dbOpts = new DatabaseOperation();

        if (dbOpts.InsertBitmapResponse(ip, screenShot))
            return true;
        else
            return false;
    }

and here is the sending side code这是发送方代码

    public bool ScreenShotResponse(string ip, byte[] ss)
    {
        response = new MyService.MasterWebService();
        try
        {
            //byte[] s = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            if (response.TakeScreenShotResponseBack(ip, ss))
                return true;
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

I had this problem myself and it seems like the reason is that a non base64-encoded image may contain bytes that terminate a post request etc. so the request will be invalid.我自己也遇到了这个问题,原因似乎是非 base64 编码的图像可能包含终止发布请求等的字节,因此请求将无效。

Another reason may be your webprovider.另一个原因可能是您的网络提供商。 A freehoster I had tried didn't work, but when I moved to another hoster without changing a single line everything worked perfectly.我尝试过的免费主机不起作用,但是当我搬到另一个主机而没有更改任何一行时,一切都运行良好。

I recommend you to use RestSharp:我建议您使用 RestSharp:

RestRequest request = new RestRequest("url", Method.POST);
request.AddParameter("image",  Convert.ToBase64String(imagestream.ToArray());
RestClient client = new RestClient();
client.ExecuteAsync(request, (response) =>
{
    //Do something with the response
}

Try to convert your image with this:尝试使用此转换您的图像:

  public byte[] FileToByteArray(string fileName)
  {

       byte[] buff = null;

       if (fileName != null && fileName != "" && File.Exists(fileName))

       {

          buff = File.ReadAllBytes(fileName);

       }
        return buff;
    }

In my database the column for the picture is type image.I hade the same problem and i fixed it with this.在我的数据库中,图片的列是 image.I hade 相同的问题,我用这个修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM