Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:54:39+00:00 2026-06-09T14:54:39+00:00

Can anyone help with a small problem I am having, I have WCF Rest

  • 0

Can anyone help with a small problem I am having, I have WCF Rest Based Service, which has function that can accept a stream, this will be used for uplaoding images/audio/video to the server and then storing them on the server somewhere.

Testing with and image, and it appears to work, i select the image in the client, and a few seconds later the image appears on the server in the location expected, but when i try to open the image in windows picture viewer (or any image viewer), i get “No Preview Available”, and no image to view.

I am assuming it is because i am not recreating the file again correctly from the stream.

This is the method on the WCF Rest Service

    public void PutFileInFolder(int eid, Stream fileContents)
    {
        try
        {

            byte[] buffer = new byte[32768];
            MemoryStream ms = new MemoryStream();
            int bytesRead = 0;
            int totalBytesRead = 0;

            do
            {
                bytesRead = fileContents.Read(buffer, 0, buffer.Length);
                totalBytesRead += bytesRead;

                ms.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);


            //now have file in memorystream


            //save the file to the users folder 
            FileStream file = new FileStream(@"C:\bd_sites\ttgme\wwwroot\Evidence\{" + ed.LearnerID + @"}\" + ed.EvidenceFileName, FileMode.Create, System.IO.FileAccess.Write);
            byte[] bytes = new byte[ms.Length];
            ms.Read(bytes, 0, (int)ms.Length);
            file.Write(bytes, 0, bytes.Length);
            file.Close();
            ms.Close();

        }
        catch (Exception ex)
        {
            return;
        }
    }

And this is the client function for sending the file/image

    private void PostFile(EvidenceObject eo)
    {
        try
        {
            // Create the REST request. 
            string url = ConfigurationManager.AppSettings["serviceUrl"];
            string requestUrl = string.Format("{0}/PutFileInFolder/{0}", 1001);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
            request.Method = "POST";
            request.ContentType = "text/plain";

            byte[] fileToSend = File.ReadAllBytes(txtFileName.Text);
            request.ContentLength = fileToSend.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                // Send the file as body request. 
                requestStream.Write(fileToSend, 0, fileToSend.Length);
                requestStream.Close();
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);

            MessageBox.Show("File sucessfully uploaded.", "Upload", MessageBoxButton.OK, MessageBoxImage.Information);
            this.DialogResult = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error during file upload: " + ex.Message, "Upload", MessageBoxButton.OK, MessageBoxImage.Error);
        } 

    }

Also just tested a video file, the orignal file plays happily, then when i upload it through the service, the file that is created on the server wont play.

I am sure it is somemthing really dumb i am doing, but any help is really appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:54:41+00:00Added an answer on June 9, 2026 at 2:54 pm

    The problem was the way i was writing to the file stream, i wasnt actually passing out the bytes of the file, but rather the new bytes making the file the same size but with basically no contents of the original file.

    this was the change to the code

    //FileStream file = new FileStream(@"C:\bd_sites\ttgme\wwwroot\Evidence\{" + ed.LearnerID + @"}\" + ed.EvidenceFileName, FileMode.Create, System.IO.FileAccess.Write);
    //byte[] bytes = new byte[ms.Length];
    ////ms.Read(buffer, 0, (int)ms.Length);
    //file.Write(bytes, 0, bytes.Length);
    //file.Close();
    //ms.Close();
    
    using (FileStream fs = File.OpenWrite(@"C:\bd_sites\ttgme\wwwroot\Evidence\{" + ed.LearnerID + @"}\" + ed.EvidenceFileName))
    {
        ms.WriteTo(fs);
        fs.Close();
        ms.Close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help with a small problem. I've got a nice simple CSS dropdown
can anyone help, i have a small issue, i have an interface and also
Can anyone help me find an up-to-date, working ATL project which has a main
can anyone help? I have small procedure to read in an UTF-8 file with
having a small problem. I'm trying to create a loop that will create an
I would like to know if anyone can help me out with a problem
Hey, I'm having a strange issue. I'm wondering if anyone can help me make
Hi all can anyone help me out with the solution to this problem... I
I am having a very common problem which it seems that all the available
EDIT* Answer below fixed the problem if anyone has the same problems. Small problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.