2016-03-20 8 views
-2

Ich versuche ein Bild von Android zu Node Server senden. Was ich tat:Multipart Anfrage über Android - Node.js

 try{ 
      //------------------ CLIENT REQUEST 
      FileInputStream fileInputStream = new FileInputStream(new File(existingFileName)); 
      // open a URL connection to the Servlet 
      URL url = new URL(urlString); 
      // 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. 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Connection", "Keep-Alive"); 
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
      dos = new DataOutputStream(conn.getOutputStream()); 
      dos.writeBytes(twoHyphens + boundary + lineEnd); 

      dos.writeBytes("Content-Disposition: form-data; name=\"image_upload\";originalFilename=\"" + params[1] + "\";path=\"" + params[0] + "\"" + lineEnd); // originalFilename is the Name of the File to be uploaded 
      dos.writeBytes(lineEnd); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 
      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); 
      } 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 
     } 

I image_upload Dateiparameter in Knoten als files.image_upload[0].originalFilename und files.image_upload[0].path

Problem bei image_upload[0] geschieht immer bin. Server stürzt ab und sagt TypeError: Cannot read property '0' of undefined

+0

Es würde wirklich helfen, wenn Sie den Grund von downvote angeben könnten, damit ich meine nächsten Fragen verbessern kann. –

Antwort

0

Geben Sie Mime-Typ zu Ihrer Bilddatei.

0

Ersetzt einen Teil des Codes zu diesem und es funktioniert jetzt.

 dos.writeBytes("Content-Disposition: form-data; name=\"image_upload\"; filename=\""+params[1]+"\""+lineEnd); 
     dos.writeBytes("Content-Type: \"undefined\""+lineEnd); 
     dos.writeBytes(lineEnd); 
Verwandte Themen