Search code examples
c#httpwebrequestmultipartform-dataptc-windchill

Upload file in server with a multipart/form-data - C#


I want to upload a file in WindChill (is a PLM from PTC). They give to us an REST API with the services to do this. They split an upload of file in 3 stages.

  • Stage 1 - We call a service where we give the number of files to upload. In this case only one.
  • Stage 2 - A multipart/formdata where we give the file to upload.
  • Stage 3 - The last stage where we give the file name, the file size etc...

I think my problem is on stage 2. All the stages run successfully but when i try to open the uploaded file, in this case a pdf, the file is blank, but with the same number of pages of the original one. I compare the content of the uploaded file with the original one and the content inside is the same with a big difference. The original is with an ANSI encoding while the uploaded one is with the UTF-8 encoding. So, I think my problem is on the stage 2.

I'm with some doubts on this stage. In C# I get the bytes[] of file, but in the end I need to pass this bytes to a string to send in a multipart form. What is the encoding that i should use to get string? I tested with default, UTF-8, UNICODE, ASCII encoding but nothing. Here is the example of the Post request body. In a C# I use the HTTPWebRequest to make a request.

------boundary
Content-Disposition: form-data; name="Master_URL"

https://MyUrl/Windchill/servlet/WindchillGW
------boundary
Content-Disposition: form-data; name="CacheDescriptor_array"

844032:844032:844032;
------boundary
Content-Disposition: form-data; name="844032"; filename="newDoc.pdf"
Content-Type: application/pdf

%PDF-1.7  //// The content of the file starts here
%µµµµ
1 0 obj
........

------boundary--

Before this approach I tried to convert the bytes[] ToBase64String and send an body like this:

------boundary
Content-Disposition: form-data; name="Master_URL"

https://MyUrl/Windchill/servlet/WindchillGW
------boundary
Content-Disposition: form-data; name="CacheDescriptor_array"

844033:844033:844033;
------boundary
Content-Disposition: form-data; name="844033"; filename="newDoc.pdf"
Content-Type: application/pdf
Content-Transfer-Encoding: base64

JVBERi0xLjcNCiW1tbW1DQox ........ //// The content of the file starts here 
------boundary--

In this case, when I try to open the file i get the error "Failed to load PDF document". The file is corrupt.

I think the problem is on the stage 2, but I will share the body that i send in last stage for your understanding.

{"ContentInfo":[{"StreamId":844034,"EncodedInfo": "844034%3A40384%3A9276564%3A844034","FileName": "newDoc.pdf","PrimaryContent": true,"MimeType" : "application/pdf","FileSize" : 40384}]}

The StreamId and the EncodedInfo are returns of the stage 2 that I need to provide in the stage 3.

Anyone can see what I'm doing wrong? Anyone have some tips to help me to solve this issue?

Many thanks.


Solution

  • I have a big tip for solve issues like that. Use postman. Make all the job in postman. After your job is done, you can generate the code for multiple languages with postman. Many thanks.

    Postman

    Postman