Saturday, May 25, 2013

How to uploading Image/file from Android to .Net WCF And send XML file also.

With the help of following code or link we can easily upload image or any file from Android to .NET WCF.

This is code to upload image on .net wcf

    URL url = null;
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;
        String existingFileName = null;

        existingFileName = my_gallary_selected_img_path;
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = Integer.MAX_VALUE;
        String responseFromServer = "";

       url = new URL(my_webservice);

        try {
                   FileInputStream fileInputStream = new FileInputStream(new File(
                    existingFileName));

            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.

            StringBuilder sb = new StringBuilder();

            boolean genderStatus;
            if (radioGenderButton.getText().toString().trim().equals("Male")) {
                genderStatus = true;
            } else {
                genderStatus = false;
            }

            // sb.append("<Result> <ServiceAuthToken>XadGenAuth123</ServiceAuthToken> <UserAuthToken>e6c53849-d78b-49cf-b59f-7d36b19bd220</UserAuthToken> <ContactId>19</ContactId> <ContactsXml> <Contact> <ContactId>19</ContactId> <FirstName>Ranjit</FirstName>    <LastName>Chandel</LastName> <Gender>true</Gender>    <Age>37</Age>  <Company>RedOrange</Company>    <Email>vikas@test.com</Email> <Address>Chs</Address>    <State>Chandigarh</State>    <Zip>42342355</Zip>    <Country>United States</Country>    <SkypeId />    <Interest>1-2-3</Interest>    </Contact>    </ContactsXml>    </Result>");
            conn.setRequestMethod("POST");
            conn.setRequestProperty("ContactXmlFile", "" + sb);
           
            dos = new DataOutputStream(conn.getOutputStream());
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            }
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (MalformedURLException ex) {
            Log.e("Debug", "error: " + ex.getMessage(), ex);
        } catch (IOException ioe) {
            Log.e("Debug", "error: " + ioe.getMessage(), ioe);
        }
        // ITS SERVER RESPONSE
        try {
            inStream = new DataInputStream(conn.getInputStream());
            String str;

            while ((str = inStream.readLine()) != null) {
                Log.e("Debug", "Server Response " + str);
                // statuss.setText(str);
            }
            inStream.close();

        } catch (IOException ioex) {
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        }

Reference from:
Please check this stack overflow link also:
http://stackoverflow.com/questions/7860298/uploading-ms-word-files-from-android-to-net-wcf/8209430#8209430

No comments:

Post a Comment

Post a Comment