用工小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
6.5 KiB

6 months ago
  1. # formstream
  2. [![NPM version][npm-image]][npm-url]
  3. [![CI](https://github.com/node-modules/formstream/actions/workflows/ci.yml/badge.svg)](https://github.com/node-modules/formstream/actions/workflows/ci.yml)
  4. [![Test coverage][codecov-image]][codecov-url]
  5. [![npm download][download-image]][download-url]
  6. [npm-image]: https://img.shields.io/npm/v/formstream.svg?style=flat-square
  7. [npm-url]: https://npmjs.org/package/formstream
  8. [codecov-image]: https://codecov.io/github/node-modules/formstream/coverage.svg?branch=master
  9. [codecov-url]: https://codecov.io/github/node-modules/formstream?branch=master
  10. [download-image]: https://img.shields.io/npm/dm/formstream.svg?style=flat-square
  11. [download-url]: https://npmjs.org/package/formstream
  12. A [multipart/form-data](http://tools.ietf.org/html/rfc2388) encoded stream, helper for file upload.
  13. ## Install
  14. ```bash
  15. npm install formstream
  16. ```
  17. ## Quick Start
  18. ```js
  19. var formstream = require('formstream');
  20. var http = require('http');
  21. var form = formstream();
  22. // form.file('file', filepath, filename);
  23. form.file('file', './logo.png', 'upload-logo.png');
  24. // other form fields
  25. form.field('foo', 'fengmk2').field('love', 'aerdeng');
  26. // even send file content buffer directly
  27. // form.buffer(name, buffer, filename, mimeType)
  28. form.buffer('file2', new Buffer('This is file2 content.'), 'foo.txt');
  29. var options = {
  30. method: 'POST',
  31. host: 'upload.cnodejs.net',
  32. path: '/store',
  33. headers: form.headers()
  34. };
  35. var req = http.request(options, function (res) {
  36. console.log('Status: %s', res.statusCode);
  37. res.on('data', function (data) {
  38. console.log(data.toString());
  39. });
  40. });
  41. form.pipe(req);
  42. ```
  43. ### Chaining
  44. ```js
  45. var fs = require('fs');
  46. var formstream = require('formstream');
  47. var filepath = './logo.png';
  48. fs.stat(filepath, function (err, stat) {
  49. formstream()
  50. .field('status', 'share picture')
  51. .field('access_token', 'your access token')
  52. .file('pic', filepath, 'logo.png', stat.size)
  53. .pipe(process.stdout); // your request stream
  54. });
  55. ```
  56. ### Set min chunk buffer size
  57. Some web servers have a limit on the number of chunks, and you can set `minChunkSize` to ensure the size of chunk sent to the server.
  58. ```js
  59. var fs = require('fs');
  60. var FormStream = require('formstream');
  61. var filepath = './big-file.zip';
  62. fs.stat(filepath, function (err, stat) {
  63. new FormStream({
  64. // send >= 2MB chunk buffer size to the server
  65. minChunkSize: 1024 * 1024 * 2,
  66. }).field('status', 'share file')
  67. .field('access_token', 'your access token')
  68. .file('file', filepath, 'big-file.zip', stat.size)
  69. .pipe(process.stdout); // your request stream
  70. });
  71. ```
  72. ## API Doc
  73. ### formstream([options])
  74. Create a form instance.
  75. #### Arguments
  76. - **options.minChunkSize** Number - min chunk size to emit data event
  77. #### Returns
  78. Form - form instance
  79. ### FormStream#field(name, value)
  80. Add a normal field to the form.
  81. #### Arguments
  82. - **name** String - Name of field
  83. - **value** String - Value of field
  84. #### Returns
  85. Form - form instance
  86. ### FormStream#file(name, filepath[, filename][, filesize])
  87. Add a local file to be uploaded to the form.
  88. #### Arguments
  89. - **name** String - Name of file field
  90. - **filepath** String - Local path of the file to be uploaded
  91. - ***filename*** String - Optional. Name of the file (will be the base name of `filepath` if empty)
  92. - ***filesize*** Number - Optional. Size of the file (will not generate `Content-Length` header if not specified)
  93. #### Returns
  94. Form - form instance
  95. ### FormStream#buffer(name, buffer, filename[, contentType])
  96. Add a buffer as a file to upload.
  97. #### Arguments
  98. - **name** String - Name of field
  99. - **buffer** Buffer - The buffer to be uploaded
  100. - **filename** String - The file name that tells the remote server
  101. - ***contentType*** String - Optional. Content-Type (aka. MIME Type) of content (will be infered with `filename` if empty)
  102. #### Returns
  103. Form - form instance
  104. ### FormStream#stream(name, stream, filename[, contentType][, size])
  105. Add a readable stream as a file to upload. Event 'error' will be emitted if an error occured.
  106. #### Arguments
  107. - **name** String - Name of field
  108. - **stream** [stream.Readable](http://nodejs.org/api/stream.html#stream_class_stream_readable) - A readable stream to be piped
  109. - **filename** String - The file name that tells the remote server
  110. - ***contentType*** String - Optional. Content-Type (aka. MIME Type) of content (will be infered with `filename` if empty)
  111. - ***size*** Number - Optional. Size of the stream (will not generate `Content-Length` header if not specified)
  112. #### Returns
  113. Form - form instance
  114. ### FormStream#headers([headers])
  115. Get headers for the request.
  116. #### Arguments
  117. - **headers** Object - Additional headers
  118. #### Example
  119. ```js
  120. var headers = form.headers({
  121. 'Authorization': 'Bearer kei2akc92jmznvnkeh09sknzdk',
  122. 'Accept': 'application/vnd.github.v3.full+json'
  123. });
  124. ```
  125. #### Returns
  126. Object - Headers to be sent.
  127. ### Event 'error'
  128. Emitted if there was an error receiving data.
  129. ### Event 'data'
  130. The 'data' event emits when a Buffer was used.
  131. See [Node.js Documentation](http://nodejs.org/api/stream.html#stream_event_data) for more.
  132. ### Event 'end'
  133. Emitted when the stream has received no more 'data' events will happen.
  134. See [Node.js Documentation](http://nodejs.org/api/stream.html#stream_event_end) for more.
  135. ## License
  136. [MIT](LICENSE)
  137. <!-- GITCONTRIBUTOR_START -->
  138. ## Contributors
  139. |[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/288288?v=4" width="100px;"/><br/><sub><b>xingrz</b></sub>](https://github.com/xingrz)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/13151189?v=4" width="100px;"/><br/><sub><b>fjc0k</b></sub>](https://github.com/fjc0k)<br/>|[<img src="https://avatars.githubusercontent.com/u/18096247?v=4" width="100px;"/><br/><sub><b>mrspeiser</b></sub>](https://github.com/mrspeiser)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|
  140. | :---: | :---: | :---: | :---: | :---: | :---: |
  141. [<img src="https://avatars.githubusercontent.com/u/7326406?v=4" width="100px;"/><br/><sub><b>shaozj</b></sub>](https://github.com/shaozj)<br/>
  142. This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed May 15 2024 00:34:12 GMT+0800`.
  143. <!-- GITCONTRIBUTOR_END -->