0

Being surfing for last 3-4 days downloading, running and fixing issues with available demo projects online, none of them work so far.

I need to upload an image using WCF webservice. Where from client side end I like to upload it by means of form (multipart/form-data), including some file description.

Any solution working with proper answer? My mind is really stacked overflow trying different solution. One which I initially have I am able to upload a text file where file gets created with some extra content in it. I need to upload image file.

------------cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5
Content-Disposition: form-data; name=\"Filename\"

testing file gets upload...

When I upload image file, the image file is empty.

Initial Code (one implantation), method by means of which I get the .txt file as above, in case of image its blank (or say corrupt don't know)

    private string uplaodFile(Stream stream)
    {
        StreamReader sr = new StreamReader(stream);
        int length = sr.ReadToEnd().Length;
        byte[] buffer = new byte[length];
        stream.Read(buffer, 0, length);
        FileStream f = new FileStream(Path.Combine(HostingEnvironment.MapPath("~/Upload"), "test.png"), FileMode.OpenOrCreate);
        f.Write(buffer, 0, buffer.Length);
        f.Close();
        stream.Close();
        return "Recieved the image on server";
    }

another;

public Stream FileUpload(string fileName, Stream stream)
        {
            string FilePath = Path.Combine(HostingEnvironment.MapPath("~/Upload"), fileName);
            int length = 0;
            using (FileStream writer = new FileStream(FilePath, FileMode.Create))
            {
                int readCount;
                var buffer = new byte[8192];
                while ((readCount = stream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    writer.Write(buffer, 0, readCount);
                    length += readCount;
                }
            }

            return returnJson(new { resp_code = 302, resp_message = "occurred." });
        }
KumailR
  • 505
  • 1
  • 4
  • 20
  • *Don't* use WCF for this. WCF means SOAP. Uploading a file isn't a job for a web service, it's a job for plain-old HTTP POST. What you ask is no different than any file upload form. – Panagiotis Kanavos May 29 '18 at 10:45
  • There is not enough information here to answer. Please show code. – Tom W May 29 '18 at 10:46
  • @PanagiotisKanavos not necessarily, the WebHttpBinding supports RESTful services, but it's certainly not the easiest way of doing REST in .NET – Tom W May 29 '18 at 10:47
  • @TomW there are lots of code already on the stackoverflow(on internet) I have tried already, I want some proper working solution with details. If you could help. – KumailR May 29 '18 at 11:03
  • @smkrn110 post **your** code. There's no way anyone can help if you won't specify what causes the problem. – Tom W May 29 '18 at 11:05
  • @TomW have update the question with the code. Some other (https://www.codeproject.com/Articles/797979/Uploading-Downloading-a-file-using-WCF-REST-servic) – KumailR May 29 '18 at 11:11
  • You're creating an empty file then writing its contents (nothing) to another file. Please post the code you are using to receive a file upload, so that's the WCF contract interface and the service implementation. – Tom W May 29 '18 at 11:18
  • update the code, which was working apology for put random code as I was hit and try. – KumailR May 30 '18 at 05:15
  • @TomW any hint what was wrong or if you have any better solution? – KumailR Jun 01 '18 at 05:31
  • You're trying to read the incoming stream twice - once with `StreamReader.ReadToEnd` and again with `stream.Read`. You can't do that, (network) streams are not replayable. Instead initialise a fixed length buffer and read from the incoming stream.and write to the file stream in a loop, there are innumerable examples of this on the web – Tom W Jun 01 '18 at 06:15
  • @TomW Kindly rectify the issue place another; its given me the same result blank image file. – KumailR Jun 05 '18 at 07:36
  • Your new code snippet looks fine to me. If you debug into it, can you see the loop being executed and bytes being written? – Tom W Jun 09 '18 at 13:50
  • Its not working as told, already I tried different approaches even verify web.config according with each approaches. – KumailR Jun 12 '18 at 06:29
  • Hey @TomW Issue is fixed, actually I did not focus what I have posted, as I removed the above text content disposition .... the image gets recover, thanks so far being responsive. – KumailR Jun 12 '18 at 06:50

0 Answers0