0

I've been working with this Android app that uploads image on a server. I was able to upload successfully using PHP but I'm having trouble uploading it on a .net webservice. The guys who is in charge of the webservice gave the code to me so I can have a looked at it.

Here it is.

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;

        }

Problem is I don't have experience in .net so I'm not sure how to handle this situation. It seems the upload image function in the webservice use a fileStream as you can see from the parameters above.

EDIT

Here's my java code.

HttpResponse httpResponse = null;
        InputStream inputStream;
        try {
            inputStream = new FileInputStream(new File(filePath));
            byte[] data;
            try {
                data = IOUtils.toByteArray(inputStream);

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(
                        "http://localhost/fileUpload");

                InputStreamBody inputStreamBody = new InputStreamBody(
                        new ByteArrayInputStream(data), fileName);
                MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
                        .create();
                multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                multipartEntity.addPart("file", inputStreamBody);
                HttpEntity entity = multipartEntity.build();
                httpPost.setEntity(entity);
                httpResponse = httpClient.execute(httpPost);

                if (httpResponse != null) {

                } else {

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
4

1 回答 1

0

I found a tutorial here. I wasn't successful in uploading the file but that's for another question.

于 2014-08-07T06:23:48.067 回答