yuyanzhizi 2013-12-17 06:43
浏览 3275

用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;
        }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?