2

I'm trying to write a WCF Restful api to upload file to a server. I've gone through the posts for Multipart Parser, and tried to use them. The problem I'm facing is that when reading the stream content, some special characters in the image file gets replaced and I end up with a file almost twice the original size.

example: Below is one line from the image file

-¦ã09°O ð -ÍQa:BÐ`QËÎâ<Îä\Îæ|ÎèœÎê¼Îì,    ;

This gets converted to

�-���09�O � -�Qa:B�`Q���<��\��|������,  �  ;

My assumption was that this had something to do with the encoding used to read the stream, so I've tried to use StreamReader for this, but with no success.

MultipartParser parser;
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
{
    var encoding = reader.CurrentEncoding;
    parser.Parse(stream, encoding);
}

Edited:

UI piece

<form id="testForm" action="--service url--" method="POST" enctype="multipart/form-data">
    <table>            
        <tr><td>File:</td><td><input type="file" name="Contents"></td></tr>
        <tr><td/><td><input type="submit" value="Send"></td></tr>
    </table>
</form>

Here's my request from fiddler

Cache-Control: max-age=0
Content-Length: 278783
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryokLIHmhMy8bbelA6
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

------WebKitFormBoundaryokLIHmhMy8bbelA6
Content-Disposition: form-data; name="Contents"; filename="someimage.jpg"
Content-Type: image/jpeg

--file content here---

------WebKitFormBoundaryokLIHmhMy8bbelA6--
questworld
  • 21
  • 3
  • I believe for RESTfull it s better to work with text why don't you convert your image to Base64 and send it in chunks if it is too big – Coder1409 Apr 21 '15 at 08:12
  • How are you calling the RESTful service? Perhaps your HttpRequest.ContentType equals "text/plain"; ? – bit Apr 21 '15 at 08:17
  • I think we're going to need to see the client side to help you out here – Nils O Apr 21 '15 at 08:23
  • Thanks for you reply guys. I've edited the description with the UI code and fiddler request. – questworld Apr 21 '15 at 08:28
  • See this once: http://stackoverflow.com/questions/20508788/do-i-need-content-type-application-octet-stream-for-file-download – bit Apr 21 '15 at 08:54

1 Answers1

0

Thanks for the help guys, but it seems the issue was with the encoding. Using this: Encoding.GetEncoding("iso-8859-1"), and some trimming resolved it.

questworld
  • 21
  • 3