Sets the data content for this MIME part from a string or Buffer.
The data as a string or Buffer, or null to reset
this for method chaining
Sets a callback-based mechanism for supplying part content dynamically.
Expected total size in bytes, -1 works for unknown sizes
Object containing read, seek (optional), and free (optional) callbacks
this for method chaining
Use this for streaming or dynamically generated content. The read callback
will be called multiple times to supply data chunks. Return null from
the read callback to signal EOF.
let currentOffset = 0
const data = Buffer.from('Large data to stream...')
part
.setName('stream')
.setDataCallback(data.length, {
read: (size) => {
if (currentOffset >= data.length) return null // EOF
const chunk = data.slice(currentOffset, currentOffset + size)
currentOffset += chunk.length
return chunk
},
seek: (offset, origin) => {
if (origin === 0) { // SEEK_SET
currentOffset = offset
return true
}
return false
}
})
Sets the data content for this MIME part from a Node.js Readable stream.
Node.js Readable stream to read data from
Callback function to unpause the transfer when data becomes available
Optionalsize: numberOptional expected total size in bytes (for Content-Length header)
this for method chaining
This method provides a wrapper around setDataCallback for working with
Node.js streams. The stream is kept in paused mode and data is read synchronously
when curl requests it. When no data is available, the transfer is paused using
CurlReadFunc.Pause, and the unpause callback is invoked when data becomes
available to resume the transfer.
The unpause function should unpause the curl handle's receive operation, typically
by calling handle.pause(handle.pauseFlags & ~CurlPause.Recv).
For very large files, consider using setFileData instead, as it streams directly from disk without going through Node.js streams.
Stream from file with unpause:
import { createReadStream } from 'fs'
import { CurlPause } from 'node-libcurl'
const stream = createReadStream('/path/to/file.txt')
const curl = new Curl()
const mime = new CurlMime(curl)
mime
.addPart()
.setName('document')
.setDataStream(stream, () => {
curl.pause(curl.handle.pauseFlags & ~CurlPause.Recv)
})
.setType('text/plain')
Stream with known size:
import { createReadStream, statSync } from 'fs'
import { CurlPause } from 'node-libcurl'
const filepath = '/path/to/file.txt'
const size = statSync(filepath).size
const stream = createReadStream(filepath)
const curl = new Curl()
const mime = new CurlMime(curl)
mime
.addPart()
.setName('document')
.setDataStream(
stream,
() => curl.pause(curl.handle.pauseFlags & ~CurlPause.Recv),
size
)
Sets the content transfer encoding for this part.
Encoding scheme: 'binary', '8bit', '7bit', 'base64', 'quoted-printable', or null to disable
this for method chaining
Sets the remote filename for this part.
this for method chaining
This appears in the Content-Disposition header and can be different from the actual local filename when using setFileData.
Sets the field name for this MIME part.
The field name, or null to reset
this for method chaining
Sets a MIME structure as nested subparts of this part.
A CurlMime instance to use as subparts, or null to reset
this for method chaining
Ownership of the MIME structure transfers to this part. The subparts MIME object should not be used after this call.
Sets the content type (MIME type) for this part.
The MIME type string, or null to use default
this for method chaining
CurlMimePartclass that represents a single part in a MIME structure.This class is used to configure individual parts of a multipart form data upload. Instances are created by calling CurlMime.addPart.
Remarks
Each part can have:
All setter methods return
thisfor method chaining and throw CurlEasyError on error.Example
Basic text field with method chaining:
Example
File upload with method chaining: