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 581621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:35:55+00:00 2026-05-13T14:35:55+00:00

I am wanting to write a WCF web service that can send files over

  • 0

I am wanting to write a WCF web service that can send files over the wire to the client. So I have one setup that sends a Stream response. Here is my code on the client:

private void button1_Click(object sender, EventArgs e)
{
    string filename = System.Environment.CurrentDirectory + "\\Picture.jpg";
    if (File.Exists(filename))
        File.Delete(filename);

    StreamServiceClient client = new StreamServiceClient();

    int length = 256;
    byte[] buffer = new byte[length];
    FileStream sink = new FileStream(filename, FileMode.CreateNew, FileAccess.Write);
    Stream source = client.GetData();
    int bytesRead;
    while ((bytesRead = source.Read(buffer,0,length))> 0)
    {
        sink.Write(buffer,0,length);
    }
    source.Close();
    sink.Close();
    MessageBox.Show("All done");
}

Everything processes fine with no errors or exceptions. The problem is that the .jpg file that is getting transferred is reported as being “corrupted or too large” when I open it.

What am I doing wrong?

On the server side, here is the method that is sending the file.

public Stream GetData()
{
    string filename = Environment.CurrentDirectory+"\\Chrysanthemum.jpg";
    FileStream myfile = File.OpenRead(filename);
    return myfile;
}

I have the server configured with basicHttp binding with Transfermode.StreamedResponse.

  • 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-05-13T14:35:56+00:00Added an answer on May 13, 2026 at 2:35 pm

    I think the problem is this:

    while ((bytesRead = source.Read(buffer,0,length))> 0)
    {
        sink.Write(buffer,0,length);
    }
    

    Imagine you’re reading the last bit of your file – maybe it’s not 256 bytes anymore, but only 10.

    You read those last 10 bytes, bytesRead will be 10, but on the sink.Write operation, you’re still using the fixed value length (256). So when you read the last block of data, you’re writing out a block that might be too large.

    You need to change the line for the sink to:

       sink.Write(buffer, 0, bytesRead);
    

    and then it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF web service that uses ASP.NET session state. WCF sets a
I'm wanting to write a method that I can use to initialise a Map.
I am wanting to write some web services using WCF. I would like to
Within an n-tier app that makes use of a WCF service to interact with
I'm wanting to write a simple WinForms application that will be able to plot
I'm wanting to write a program in vb .net that could monitor outgoing print
I find myself wanting to write a routine that will operate on both volatile
Hello I have a simple wcf service like this, with a test method which
Often, I find myself wanting to write a unit test for a portion of
I am eventually wanting to program in Objective-C to write programs for OS X

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.