Rahul Winner
Rahul Winner

Reputation: 430

Sending Form data alongwith File Data for Content Type "application/vnd.ms-project" for MPP files

I need to send some post data along with a file stream. I'm using the following code.

This code is taken from http://technet.rapaport.com/Info/LotUpload/SampleCode/Full_Example.aspx.

private Stream GetPostStream(string filePath, Dictionary<string, string> paramMap, string boundary) {

        Stream postDataStream = new System.IO.MemoryStream();

        //adding form data
        string formDataHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
            "Content-Disposition: form-data; name=\"{0}\";" + Environment.NewLine + Environment.NewLine + "{1}";
        foreach (KeyValuePair<string, string> pair in paramMap)
        {
            byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(formDataHeaderTemplate, pair.Key, pair.Value));
            postDataStream.Write(formItemBytes, 0, formItemBytes.Length);
        }

        //adding file data
        FileInfo fileInfo = new FileInfo(filePath);

        string fileHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
        "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" +
        Environment.NewLine + "Content-Type: application/vnd.ms-project" + Environment.NewLine + Environment.NewLine;

        byte[] fileHeaderBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(fileHeaderTemplate, "UploadMPPFile", fileInfo.FullName));

        postDataStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);

        FileStream fileStream = fileInfo.OpenRead();

        byte[] buffer = new byte[1024];

        int bytesRead = 0;

        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            postDataStream.Write(buffer, 0, bytesRead);
        }

        fileStream.Close();

        byte[] endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--");
        postDataStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);

        return postDataStream;
    }

On the server side which is on JAVA, I'm using MPXJ 3rd party library to read the the file data. However, there I'm encountering the following exception. It reports some mismatch error in Header signature.

Nested exception is: net.sf.mpxj.MPXJException: Error reading file] with root cause java.io.IOException: Invalid header signature; read 0x2D2D2D2D2D2D0A0D, expected 0xE11AB1A1E011CFD0 at org.apache.poi.poifs.storage.HeaderBlockReader.(HeaderBlockReader.java:125) at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:153) at net.sf.mpxj.mpp.MPPReader.read(MPPReader.java:84)

Could anyone please help me out with this situation and suggest some solutions!

Thanks a lot.

Upvotes: 0

Views: 417

Answers (1)

Jon Iles
Jon Iles

Reputation: 2579

Looks suspiciously like the receiving end is not separating the header data you are writing to the stream from the payload data. Might be worth trying the following:

  1. Validate that you are reading the MPP file correctly by replacing the call to postDataStream.Write with a write to a local file stream. Validate that the content of the file you create is identical to the file you are reading.
  2. Replace the receiving code on the server side with something which simply echos the received data to a file so you can validate what is being received.
  3. Update the existing receiving code to write the extracted header data and the payload data to separate files to validate that both are being received correctly and that the payload filematches what is being sent from the client.

Upvotes: 0

Related Questions