Domanda

Ho lavorato con questa app Android che carica l'immagine su un server.Sono stato in grado di caricare con successo usando PHP ma ho problemi a caricarlo su un webservice .NET.I ragazzi che sono responsabili del Servizio WebService hanno dato il codice a me così posso averlo guardato.

Qui è.

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;

        }
.

Problema è che non ho esperienza in .net quindi non sono sicuro di come gestire questa situazione.Sembra che la funzione di immagine di caricamento nell'ervizio WebService utilizza un filsteam come puoi vedere dai parametri sopra.

Modifica

Ecco il mio codice Java.

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();
        }
.

È stato utile?

Soluzione

Ho trovato un tutorial qui .Non ho avuto successo nel caricare il file ma è per un'altra domanda.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top