5

I have a .net core 2.0 API service that gets two files (XML and word) and transfers them to a node js server that saves them to a MongoDB.

when I'm hosting the net core service on my localhost everything works fine but when I am uploading the .net service to open shift(Kubernete) I get an exception from the Node.js service in busboy-body-parser package in index.js file:

"Error: Unexpected end of multipart data at ...node_modules\dicer\lib\Dicer.js:62:28 at process._tickCallback (internal/process/next_tick.js61:11)"

this code caught the exception in the index.js file:

busboy.on('error', function (err) {
                debug('Error parsing form');
                debug(err);
                error = err;
                next(err);
            });

here is my c# code for posting the files to the Node.js service copied from bratched:

public byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
    {
        var request = WebRequest.Create(address);
        request.Method = "POST";
        var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        boundary = "--" + boundary;

        using (var requestStream = request.GetRequestStream())
        {
            // Write the values
            foreach (string name in values.Keys)
            {
                var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
            }

            // Write the files
            foreach (var file in files)
            {
                var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                file.Stream.CopyTo(requestStream);
                buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
            }

            var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
            requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
        }

        using (var response = request.GetResponse())
        using (var responseStream = response.GetResponseStream())
        using (var stream = new MemoryStream())
        {
            responseStream.CopyTo(stream);
            return stream.ToArray();
        }
    }
ronara
  • 336
  • 11
  • 26
  • You can use something like Fiddler to inspect the request being made and see if the request is being constructed correctly. – Nkosi Oct 31 '18 at 16:08

1 Answers1

-1

Try looking at this link: https://github.com/mscdex/dicer/issues/14

you need to add error listing to the File and see what is the problem

We can catch the error by attaching an error event handler to the file object.

file.on('error', ()=>{})

  • 4
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Jack Nov 01 '18 at 13:01