0

I have the following method used to handle file post:

        public string Message(Stream data)
        {


            int length = 0;
            using (FileStream writer = new FileStream("test.zip", FileMode.Create))
            {
                int readCount;

                var buffer = new byte[8 * 16];
                while ((readCount = data.Read(buffer, 0, buffer.Length)) != 0)
                {
                    writer.Write(buffer, 0, readCount);
                    length += readCount;
                }
            }

            return "test";


        }

Using this I can post files from Postman. When I check the zip file posted on the other side everything looks good, zip file can be opened without errors. Here is the code that I'm using to post files from C# client:

        using (WebClient clientFile = new WebClient())
        {
            clientFile.UploadFileAsync(new Uri("http://192.168.122.146:8000/Message"), "test.zip");
        }

The file is uploaded but when I want to open an error message is displayed: "The compressed (zipped) Folder is invalid or corrupted". Are there any ways to upload the file without using multipart/form-data? The code works with postman but is not working if I upload the file from my C# client. Here are the request captured from Fiddler:

POST http://192.168.122.146:8000/Message HTTP/1.1
Host: 192.168.122.146:8000
Connection: keep-alive
Content-Length: 8505677
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
Postman-Token: 4d0e5489-7818-226f-6f71-b9bee2947905
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

And the request from C# client:

POST http://192.168.122.146:8000/PutMessage HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------8d4ef8a23bf0f80
Host: 192.168.122.146:8000
Content-Length: 8505848
Expect: 100-continue
Connection: Keep-Alive

-----------------------8d4ef8a23bf0f80
Content-Disposition: form-data; name="file"; filename="test.zip"
sarbo
  • 1,661
  • 6
  • 21
  • 26
  • 1
    This will help you https://stackoverflow.com/questions/16495735/zip-file-is-getting-corrupted-after-uploaded-to-server-using-c-sharp Thanks – Amaid Niazi Aug 30 '17 at 08:24
  • Yes, the problem was that I used UploadFile method. Switching to UploadData and sending the bytes resolved the problem. Thanks! – sarbo Aug 31 '17 at 06:49

0 Answers0