1

I am using CURL command to upload a file a server that makes problem in few CLIs. Only CLIs which are CURL installed is working fine when I am running my node app.

let mnmPath = `http://xyz/api/123456/mnm-api`;
exec(`curl -X PUT -H "x-cdn-path:" ${mnmPath } --upload-file abcd.txt`, (error, stdout) => {
    if (error) {
      console.log({status: 1, message: 'Error while uploading Tarball to CDN'});
    }
    console.log({status: 0, message: 'CDN upload completed.'});
  });
AJAY KUMAR
  • 35
  • 7
  • Possible duplicate of [Uploading file using POST request in Node.js](https://stackoverflow.com/questions/25344879/uploading-file-using-post-request-in-node-js) except you are just using a PUT request instead of a POST request, requiring the change of just one keyword. – Pal Kerecsenyi Jul 03 '18 at 16:54

1 Answers1

2

You need to get the file somehow, in my example, I've assumed you have a file picker and you can access the data from that

You'll want to send your file to the server using a post request then use promises to catch or resolve based on the success of the request.

https://github.com/axios/axios

https://developer.mozilla.org/en-US/docs/Web/API/FormData

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

/*
   example of getting a file from DOM but as long as you
   pass a file to the function should be good 
*/


// es6 promise
function postFileToServer(file) {
  const formData = new FormData();
  formData.append("file", file);

  axios.post('/your-endpoint', formData)
    .then(res => /* do something with res*/ console.log(res))
    .catch(e => console.log('upload failed'))
}

function submit() {
  const file = document.getElementById("file").files;
  if (file.length > 0) {
    postFileToServer(file[0])
  }
}
input {display: block}
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
<input id="file" type="file" />
<button onclick="submit()">submit</button>

From the FormData docs

The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data".

Basically, it formats it in an easily manipulatable Object ready to be sent to an external service. You will need to check how the service you're using wants to accept the file.

Joe Warner
  • 2,928
  • 1
  • 12
  • 29
  • have i missed the point if you could explain why the downvote i'll happily change it :) – Joe Warner Jul 03 '18 at 17:01
  • I am trying to do this in node side. As I have the file inside the project structure I don't want to use the get `elementById` query. And When I use new FormData(), I am getting reference error. `ReferenceError: FormData is not defined` – AJAY KUMAR Jul 03 '18 at 17:32
  • https://www.npmjs.com/package/form-data and ignore the dom stuff then :) – Joe Warner Jul 03 '18 at 17:57
  • Thank you so much. The thing is that I am not sure what should be the expected content type when I do --upload-file through curl. https://curl.haxx.se/docs/manpage.html#-T So when try the above code I see content-type mismatch error. – AJAY KUMAR Jul 03 '18 at 19:20
  • Checked the request. content type is 'Content-Type': 'application/x-www-form-urlencoded', Not sure why it is going like this – AJAY KUMAR Jul 03 '18 at 19:29
  • Sorry I'll reply when I have time :) but will help – Joe Warner Jul 03 '18 at 20:03
  • @AJAYKUMAR hey man this post explains what content type to send files https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data – Joe Warner Jul 04 '18 at 12:14