0

服务器端代码是,

 [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "UploadDamagedImage", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json)]        
        NewClsMessage UploadDamagedImage(Details ObjDt123);

客户端代码是,

Details ObjDt = new Details();
            ObjDt.Name = "hi";
            ObjDt.Email = "hi@gmail.com";
            ObjDt.ContactNumber = "3698754215";
            ObjDt.DeviceModel = "E23";
            ObjDt.Problem = "Repair";
            ObjDt.Besttimetocontact = "9am";

在这里,我得到的错误是“对象引用未设置为对象的实例”。客户端和服务器端是否需要使用相同的对象名称?如何解决这个问题呢?

编辑

客户端代码是,

Uri uri = new Uri("http://localhost:53865/Service1.svc/UploadDamagedImage");
            Details ObjDt = new Details();
            ObjDt.Name = "hi";
            ObjDt.Email = "hi@gmail.com";
            ObjDt.ContactNumber = "3698754215";
            ObjDt.DeviceModel = "E23";
            ObjDt.Problem = "Repair";
            ObjDt.Besttimetocontact = "9am";

byte[] bytes = File.ReadAllBytes(@"D:\Projects\6mb\1.jpg");
            ObjDt.ImgUpload = bytes;
 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Details));

            MemoryStream mem = new MemoryStream();
            ser.WriteObject(mem, ObjDt);
            string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
            WebClient webClient = new WebClient();
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringAsync(uri, "POST", data);

服务器端代码是,

public NewClsMessage UploadDamagedImage(Details ObjDt123)
        {
  NewClsMessage ObjNewClsMessage = new NewClsMessage();
            try
            {
 Random rand = new Random();
                int strRandNo = rand.Next();
MemoryStream ms = new MemoryStream(ObjDt.ImgUpload);  
    Image image = Image.FromStream(ms);
}
  catch (Exception ex)
            {

            } 
}
4

0 回答 0