2

Android to WCF: Streaming multi part image and Json string

I want to create a WCF-RESTful web service method,in which i need to upload an image(multipart-form data) along with some other information (in JSON format). This web service will be accessed by android and iPhone application to send Image and json information as

I want WCF Server Side and Android Code, Help me

I'm trying to upload an image and a text to a wcf service. I create a service to save upload images:

Rest Client Code

protected void Button2_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {

                MemoryStream mStream = new MemoryStream();

                Bitmap bmpPostedImage = new Bitmap(FileUpload1.PostedFile.InputStream);

                bmpPostedImage.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);                
                byte[] imageArray = mStream.ToArray();
                mStream.Close();

                VehicleCheckListTransaction objAttachmentRequestDto = new VehicleCheckListTransaction();
                objAttachmentRequestDto.VehicleCheckListTransactionID = 0;
                objAttachmentRequestDto.VehicleCheckListID = 2;
                objAttachmentRequestDto.TTID = 10;
                objAttachmentRequestDto.UserID = 226;
                objAttachmentRequestDto.UserTypeID = 4;
                objAttachmentRequestDto.ValidTill = "215-05-10";
                objAttachmentRequestDto.CurrentDate = "215-07-22";
                objAttachmentRequestDto.CurrentStatus = "Yes";
                objAttachmentRequestDto.CreatedBy= 226;
                objAttachmentRequestDto.ModifiedBy = 226;
                objAttachmentRequestDto.CreatedDate = string.Empty;
                objAttachmentRequestDto.ModifiedDate = string.Empty;
                objAttachmentRequestDto.Notes = "Some Text";
                objAttachmentRequestDto.IsActive = true;
                objAttachmentRequestDto.FileName = "";
                objAttachmentRequestDto.FilePath = "";
                objAttachmentRequestDto.VerifiedBy = 226;

                var serializer = new DataContractJsonSerializer(typeof(VehicleCheckListTransaction));
                var ms = new MemoryStream();
                serializer.WriteObject(ms, objAttachmentRequestDto);
                ms.Position = 0;
                var reader = new StreamReader(ms);
                string requestBody = reader.ReadToEnd();

                var client = new RestClient();
                client.BaseUrl = new Uri("http://localhost:49188/WDS_SERVICE.svc/");
                var request = new RestRequest(Method.POST) { DateFormat = DataFormat.Json.ToString(), Resource = "vehiclechecklisttransaction/Add" };
                if (requestBody != null)
                    request.AddParameter("VehicleCheckListTransaction", requestBody);
                request.AddFile("file1", imageArray, "NEVER.jpg");
                var response = client.Execute(request);
            }
        }

WCF Service Code

public string UploadPhoto(Stream request)
        {
            //Read in our Stream into a string...
            StreamReader reader = new StreamReader(request);
            string JSONdata = reader.ReadToEnd();

            // ..then convert the string into a single "wsCustomer" record.
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Checklist Checklist = jss.Deserialize<Checklist>(JSONdata);

            try
            {
                FileStream targetStream = null;
                Stream sourceStream = Checklist.fileContents;

                String guid = Guid.NewGuid().ToString();

                //get photofolder path
                string photofolderName = "Trip\\Android";
                string filename = guid + ".JPEG";
                string uriPath = "file:\\C:\\inetpub\\wwwroot\\WDS\\Media\\" + photofolderName + "\\" + filename;
                string photopath = new Uri(uriPath).LocalPath;

                using (targetStream = new FileStream(photopath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    //read from the input stream in 6K chunks
                    //and save to output stream
                    const int bufferLen = 65000;
                    byte[] buffer = new byte[bufferLen];
                    int count = 0;
                    while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                    {
                        targetStream.Write(buffer, 0, count);
                    }
                    targetStream.Close();
                    sourceStream.Close();
                }

                db.Checklists.Add(Checklist);
                db.SaveChanges();
                return filename + "_" + Checklist.ChecklistID;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

0 Answers0