Solía tener este método en el proyecto, pero recientemente había un problema con el cliente. Mi colega dijo que este método nunca ha funcionado. Entonces, y tomé un día para sacar este método (creo que tardó un poco más) y compartirlo.
Código y casos de prueba:
var http = require ('http'); var ruta = require ('ruta'); var fs = require ('fs'); function postfile (fileKeyValue, req) {var boundaryKey = math.random (). toString (16); var endData = '/r/n ----' + BoundaryKey + '-'; archivos var = new Array (); for (var i = 0; i <filekeyValue.length; i ++) {var content = "/r/n ----" + boundarykey + "/r/n" + "contenido-type: application/octet-stream/r/n" + "content-disposition: form-data; name =/" " + fileKeyValue [i] .urlkey +"/"; fileNeMe =" PATH.BASEname (FileKeyValue [i] .UrlValue) + "/"/r/n " +" Contenido-transferencia de transferencia: binario/r/n/r/n "; var contentbinary = new Buffer (contenido, 'UTF-8'); // Cuando la codificación es ASCII, el chino estará confuso. files.push ({ContentBinary: ContentBinary, FilePath: FileKeyValue [i] .UrlValue}); } var contentLength = 0; for (var i = 0; i <files.length; i ++) {var stat = fs.statsync (archivos [i] .filePath); ContentLength += files [i] .ContentBinary.Length; contentLength += stat.size; } req.setheader ('content-type', 'multipart/form-data; boundary =-' + boundarykey); req.setheader ('content-longitud', contentLength + buffer.bytelength (endData)); // emite el parámetro var fileindex = 0; var doonefile = function () {req.write (files [fileIndex] .contentBinary); var fileStream = fs.CreateReadStream (files [fileIndex] .filePath, {bufferSize: 4 * 1024}); fileStream.pipe (req, {end: false}); fileStream.on ('end', function () {fileIndex ++; if (fileIndex == files.length) {req.end (endData);} else {doonefile ();}}); }; if (fileIndex == files.length) {req.end (enddata); } else {dooneFile (); } }//Test case//http://nodejs.org/api/http.html#http_http_request_options_callbackvar files = [ {urlKey: "file1", urlValue: "E://DFBF.jpg"}, {urlKey: "file2", urlValue: "E: //1.jpg"}, {urlkey: "file3", urlValue: "e: // pro space chino.mp3"}] var options = {host: "localhost", puerto: "8908", método: "post", ruta: "/home/upload"} var req = http.request (opciones, function (rese (rese) rese); console.log ('Problema con solicitud:' + e.message); console.log ("hecho");Para las pruebas del servidor, use MVC para escribir un método de carga en el controlador de inicio, y atraviese el archivo cargado y guárdelo en el disco duro.
Es solo que habrá problemas para cargar archivos grandes. Se estima que el servidor debe configurarse, así que no se preocupe por ello por ahora.
Método del lado del servidor (escribir en el controlador de inicio)
[Httppost] public string upload () {// httppostedfileBase file = this.request.files ["archivo"]; //file.saveas(file.fileName); foreach (archivo de cadena en this.request.files) {this.request.files [archivo] .saveas (@"e:/nuevo carpeta/" + this.request.files [archivo] .fileName); } return @"Guardar ruta de éxito: e:/nueva carpeta/"; }Ejecute el guión:
nodo nodejspostfile.js
Resultados de ejecución: