forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path08-file-upload-put.js
62 lines (49 loc) · 1.73 KB
/
08-file-upload-put.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Example showing how to upload a file using a `Curl` instance using http method `PUT`
*/
const path = require('path')
const fs = require('fs')
const crypto = require('crypto')
const { Curl } = require('../dist')
const curl = new Curl()
const url = 'httpbin.org/put'
const fileSize = 10 * 1024 //1KB
const fileName = path.resolve(__dirname, 'upload.test')
// write random bytes to a file, this will be our upload file.
fs.writeFileSync(fileName, crypto.randomBytes(fileSize))
console.log('File: ', fs.readFileSync(fileName, 'base64'))
fs.open(fileName, 'r+', function (err, fd) {
// enabling VERBOSE mode so we can get more details on what is going on.
curl.setOpt(Curl.option.VERBOSE, true)
// set UPLOAD to a truthy value to enable PUT upload.
curl.setOpt(Curl.option.UPLOAD, true)
// pass the file descriptor to the READDATA option
// passing one invalid value here will cause an aborted by callback error.
curl.setOpt(Curl.option.READDATA, fd)
curl.setOpt(Curl.option.URL, url)
curl.on('end', function (statusCode, body) {
console.log('Response from httpbin:')
console.log({
statusCode,
body: JSON.parse(body),
})
// remember to always close the file descriptor!
fs.closeSync(fd)
fs.unlinkSync(fileName)
// the same for the `Curl` instance, always close it when you don't need it anymore.
this.close()
})
curl.on('error', function (error, errorCode) {
console.log(error, errorCode)
fs.closeSync(fd)
fs.unlinkSync(fileName)
this.close()
})
curl.perform()
})