title: Uploading a file in Azure File Storage using node.js
body: <p>We are trying create an webservice to upload files to Azure file storage using node.js service. </p>
  
  <p>Below is the node.js server code. </p>
  
  <pre><code>exports.post = function(request, response){
  var shareName = request.headers.sharename;
  var dirPath = request.headers.directorypath;
  var fileName = request.headers.filename;
  
  var body;
  var length;
  
  request.on("data", function(chunk){
      body += chunk;
      console.log("Get data");
  });
  
  
  request.on("end", function(){
      try{
          console.log("end");
          var data = body;
          length = data.length;
  
  console.log(body); // This giving the result as undefined
  console.log(length);
  
          fileService.createFileFromStream(shareName, dirPath, fileName, body, length, function(error, result, resp) {
              if (!error) {
                  // file uploaded
                  response.send(statusCodes.OK, "File Uploaded");
              }else{
                  response.send(statusCodes.OK, "Error!");
              }
          });
  
      }catch (er) {
  response.statusCode = 400;
  return res.end('error: ' + er.message);
  }
  
  });
  
  }
  </code></pre>
  
  <p>Below is our client to upload a file.</p>
  
  <pre><code>private static void sendPOST() throws IOException {
      URL obj = new URL("https://crowdtest-fileservice.azure-mobile.net/api/files_stage/");
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
      con.setRequestMethod("POST");
      con.setRequestProperty("sharename", "newamactashare");
      con.setRequestProperty("directorypath", "MaheshApp/TestLibrary/");
      con.setRequestProperty("filename", "temp.txt");
  
  
      Path path = Paths.get("C:/Users/uma.maheshwaran/Desktop/Temp.txt");
      byte[] data = Files.readAllBytes(path);
  
      // For POST only - START
      con.setDoOutput(true);
      OutputStream os = con.getOutputStream();
      os.write(data);
      os.flush();
      os.close();
      // For POST only - END
  
      int responseCode = con.getResponseCode();
      System.out.println("POST Response Code :: " + responseCode);
  
      if (responseCode == HttpURLConnection.HTTP_OK) { // success
          BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
          String inputLine;
          StringBuffer response = new StringBuffer();
  
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
              System.out.println(inputLine);
          }
          in.close();
  
          // print result
          System.out.println(response.toString());
      } else {
          BufferedReader br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
          String line = "";
          while ((line = br.readLine()) != null) {
              System.out.println(line);
          }
          System.out.println("POST request not worked");
      }
  }
  </code></pre>
  
  <p>It is showing the error </p>
  
  <blockquote>
    <p>The request 'POST /api/files_stage/' has timed out. This could be
    caused by a script that fails to write to the response, or otherwise
    fails to return from an asynchronous call in a timely manner.</p>
  </blockquote>
  
  <p><strong>Updated:</strong> </p>
  
  <p>I have also tried below code.</p>
  
  <pre><code>  var body = new Object();
    body = request.body;
    var length = body.length;
  
    console.log(request.body);
    console.log(body);
    console.log(length);
  
      try {
          fileService.createFileFromStream(shareName, dirPath, fileName, body, length, function(error, result, resp) {
              if (!error) {
                  // file uploaded
                  response.send(statusCodes.OK, "File Uploaded");
              }else{
                  response.send(statusCodes.OK, "Error!");
              }
          });
      } catch (ex) {
              response.send(500, { error: ex.message });
      }
  </code></pre>
  
  <p>But facing the issue</p>
  
  <blockquote>
    <p>{"error":"Parameter stream for function createFileFromStream should be
    an object"}</p>
  </blockquote>
  
  <p>I am new to node.js. Please help me to fix this.</p>