0

I'm trying to upload images to External MS SQL database using android phone. I'm using Java HttpClient to send array of bytes to web server or web page. I don't know how I should approach this. The web page should be in ASP.net. I'm fairly new to ASP.Net. I did intensive research on how to read in a byte array using ASP.Net and still don't have an answer. I want my webpage or server to read in the bytes and store them into database.

Below is my Java function (it is not tested yet since I don't have a way to read bytes yet) that I want to use to send the bytes. But I have no idea how to read them in on website side. Any suggestions would be appreciated. If you guys see that I'm doing something wrong also it would be appreciated if you let me know and tell me how I should fix it. Please be specific since I'm really new to this and don't really know much about web pages. Thanks.

    private void sendImagesToServer() throws Exception
{
    ImageItem image;
    HttpURLConnection conn = null;
    ImageIterator iterator;
    DataOutputStream dos;
    byte[] byteArray;
    iterator = new ImageIterator(imageAdapter);
    String uploadUrl;

    while(iterator.hasNext())
    {
        image = iterator.getNext();
        Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(image.id));

        Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byteArray = stream.toByteArray();

        uploadUrl = "http://localhost:63776/SQLScript.aspx";

        // Send request
        try {
            // Configure connection

            URL url = new URL(uploadUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("PUT");
            conn.setRequestProperty("Connection", "Keep-Alive");

            dos = new DataOutputStream(conn.getOutputStream());
            dos.write(byteArray, 0, byteArray.length);
            dos.flush();
            dos.close();

            // Read response
            try {
                if (conn.getResponseCode() == 200) {
                    Toast.makeText(this,"Yay, We Got 200 as Response Code",Toast.LENGTH_SHORT).show();
                }
            } catch (IOException ioex) {
                ioex.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } finally{

        }   

    }

}   

1 Answer 1

0

If you've verified the bytes are getting out of the Java fine, check this question, it may have what you need.

Read Http Request into Byte array

As far as getting it into a database, you could save files in a binary database (different MSSQL setup) or convert to strings and back again as necessary.

Sign up to request clarification or add additional context in comments.

2 Comments

Does ASP.Net automatically call processRequest or how do I call that function? how do I know im getting a request?
Well I guess i just might have to go back to old tcp/ip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.