Question

It's been days and I am still stuck with this image upload thing and I can't get this to work. I will not be uploading any of my source code since it looks pathetic and not working so I have to beg someone who can make this work for me. I've been Googling around for a while but can't seem to make any of them work.

Basically what I am trying to do is upload a Bitmap to the server and it has to be sent as fileStream as you can see below. That's all.

Here is the source code for the web service anyway. This will be fired up when HttpPost request is made from the Android device. Take note that this is not my code. Someone else is in charge of this.

public Stream FileUpload(string fileName, Stream fileStream){
       var serverPath = System.Web.Hosting.HostingEnvironment.MapPath("~/FileUpload/");
       if (File.Exists(serverPath + fileName)) File.Delete(serverPath + fileName); // delete file if already used


       //FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);
       FileStream fileToupload = new FileStream(serverPath + fileName, FileMode.Create);

       byte[] bytearray = new byte[10000];//
       int bytesRead, totalBytesRead = 0;
       do
       {
           bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
           totalBytesRead += bytesRead;
       } while (bytesRead > 0);

       fileToupload.Write(bytearray, 0, bytearray.Length);
       fileToupload.Close();
       fileToupload.Dispose();

       FileStream fs = File.OpenRead(serverPath + fileName);
       WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
       return fs;
   }

Any help is much appreciated.

Was it helpful?

Solution

This is how I do it, that you of course run in a background task

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "***1234321**";
    String filePath = "path/to/file";
    String fileName = new File(filePath).getName();
    StringBuffer response =new StringBuffer();

    try {
        URL connectUrl = new URL((String) params[0]);
        FileInputStream fileInputStream = new FileInputStream(filePath);
        HttpURLConnection conn = (HttpURLConnection) connectUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\""+ params[2] +"\"; filename=\"" + fileName +"\"" + lineEnd);
        dos.writeBytes("Content-Type: application/octet-stream" + lineEnd);
        dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bufferSize];

        // read file and write it into form...
        int 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);
        }

        // send multipart form data necessary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        fileInputStream.close();
        dos.flush();


        dos.close();

    } catch (Exception e) {

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top