2 yuyanzhizi yuyanzhizi 于 2013.12.17 14:43 提问

用web api下载服务器图片时传输的byte[]数组变化了。我的目的是接收到正确的byte[]

用web api下载服务器图片时传输的byte[]数组变化了。(上面从sql出来的byte[]是正确的,下面的接收到的byte[]就有问题了)传输用的是json,下面的是发送的代码和接受的代码,求各位大神解答,自己觉得是发送的方式有问题。数据流是SQl中的image类型

[HttpGet]
public byte[] GoodsPhoto(Int32 goodsid)
{
try
{
//DataTable goodsTable = new DataTable();
//byte[] a = new byte[10];
using (SqlConnection cn = new SqlConnection(DBHelper.Instance.ConnectionString))
{
String sql = "select photo from Bill_GoodsPhoto where goodsid=6";

                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.AddWithValue("goodsid", goodsid);
                cn.Open();

                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    FileStream fs = new FileStream(@"f:\1.jpg", FileMode.Create, FileAccess.Write);

                  byte[] bytes = (byte[])dr["Photo"];
                    //MemoryStream ms = new MemoryStream(a, 0, a.Length);
                    //Image returnImage = Image.FromStream(ms);
                    //returnImage.Save(@"f:\1.jpg");
                    return bytes;
                }


                //goodsTable.Load(dr);

                //return new Result<DataTable>(goodsTable);
                return null;

            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }

[HttpGet]
public byte[] GoodsPhoto11(Int32 goodsid)
{

        try
        {
            String URL = "http://192.168.1.106:6971/API/Epoch/GoodsPhoto?goodsid=6";

            WebClient wc = new WebClient();
            byte[] ab = wc.DownloadData(URL);

            MemoryStream ms = new MemoryStream(ab, 0, ab.Length);
            Image returnImage = Image.FromStream(ms);

            returnImage.Save(@"f:\1.jpg");
            return ab;

        }
        catch (Exception ex)
        {
            return null;
        }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!