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

  • SEARCH
  • Home
  • 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 7634903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:18:09+00:00 2026-05-31T07:18:09+00:00

The file succeed to upload when it is 2KB or lower in size. The

  • 0

The file succeed to upload when it is 2KB or lower in size. The main reason why I use streaming is to be able to upload file up to at least 1 GB. But when I try to upload file with less 1MB size, I get bad request. It is my first time to deal with downloading and uploading process, so I can’t easily find the cause of error.

Testing part:

private void button24_Click(object sender, EventArgs e)
{
    try
    {
        OpenFileDialog openfile = new OpenFileDialog();
        if (openfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string port = "3445";
            byte[] fileStream;
            using (FileStream fs = new FileStream(openfile.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                fileStream = new byte[fs.Length];
                fs.Read(fileStream, 0, (int)fs.Length);
                fs.Close();
                fs.Dispose();
            }

            string baseAddress = "http://localhost:" + port + "/File/AddStream?fileID=9";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress);
            request.Method = "POST";
            request.ContentType = "text/plain";
            //request.ContentType = "application/octet-stream";
            Stream serverStream = request.GetRequestStream();
            serverStream.Write(fileStream, 0, fileStream.Length);
            serverStream.Close();
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                int statusCode = (int)response.StatusCode;
                StreamReader reader = new StreamReader(response.GetResponseStream());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}  

Service:

[WebInvoke(UriTemplate = "AddStream?fileID={fileID}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
public bool AddStream(long fileID, System.IO.Stream fileStream)
{
    ClasslLogic.FileComponent svc = new ClasslLogic.FileComponent();
    return svc.AddStream(fileID, fileStream);
}  

Server code for streaming:

namespace ClasslLogic
{
    public class StreamObject : IStreamObject
    {
        public bool UploadFile(string filename, Stream fileStream)
        {
            try
            {
                FileStream fileToupload = new FileStream(filename, FileMode.Create);
                byte[] bytearray = new byte[10000];
                int bytesRead, totalBytesRead = 0;
                do
                {
                    bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
                    totalBytesRead += bytesRead;
                } while (bytesRead > 0);

                fileToupload.Write(bytearray, 0, bytearray.Length);
                fileToupload.Close();
                fileToupload.Dispose();
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
            return true;
        }
    }
}  

Web config:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding>
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2097152" maxBytesPerRead="4096" maxNameTableCharCount="2097152" />
      <security mode="None" />
    </binding>
    <binding name="ClassLogicBasicTransfer" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:15:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="67108864" maxReceivedMessageSize="67108864" messageEncoding="Mtom" textEncoding="utf-8" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="67108864" maxBytesPerRead="4096" maxNameTableCharCount="67108864" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
    <binding name="BaseLogicWSHTTP">
      <security mode="None" />
    </binding>
    <binding name="BaseLogicWSHTTPSec" />
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>

I’m not sure if this affects the streaming function, because I’m using WCF4.0 rest template
which config is dependent in Global.asax.

One more thing is this, whether I run the service and passing a stream or not, the created file always contain this thing.

enter image description here

How could I remove the “NUL” data?

Thanks in advance.

Edit

public bool UploadFile(string filename, Stream fileStream)
        {
            try
            {
                FileStream fileToupload = new FileStream(filename, FileMode.Create);
                byte[] bytearray = new byte[10000];
                int bytesRead, totalBytesRead = 0;
                do
                {
                    bytesRead = fileStream.Read(bytearray, totalBytesRead, bytearray.Length - totalBytesRead);
                    totalBytesRead += bytesRead;

                } while (bytesRead > 0);

                fileToupload.Write(bytearray, 0, totalBytesRead);
                fileToupload.Close();
                fileToupload.Dispose();
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
            return true;
        }
  • 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-31T07:18:10+00:00Added an answer on May 31, 2026 at 7:18 am

    I think the reason you’re seeing nulls is because of the following line. You’re initializing a buffer, but you’re probably not filling that entire buffer:

    fileToupload.Write(bytearray, 0, bytearray.Length);
    fileToupload.Close();
    fileToupload.Dispose();
    

    It should instead be:

    fileToupload.Write(bytearray, 0, totalBytesRead);
    fileToupload.Close();
    fileToupload.Dispose();
    

    You’re also always overwriting your buffer within each loop iteration:

    fileStream.Read(bytearray, 0, bytearray.Length)`.  
    

    When you probably want this, as it continues to fill the rest of your buffer:

    fileStream.Read(bytearray, totalBytesRead, byteArray.Length - totalBytesRead);
    

    However, if you move the code that writes to the file inside your loop, then you will only have to make minimal changes to your existing code:

    int bytesRead, totalBytesRead = 0;
    do
    {
        bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
        if (bytesRead > 0)
        {
            fileToupload.Write(bytearray, 0, bytesRead);
        }
    } while (bytesRead > 0);
    

    You will receive 400 if you try to send more data than the MaxReceivedMessageSize. Adjust it accordingly:

    <binding maxReceivedMessageSize="10485760">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to encode the × character in a SJIS encoded file... but it
I searched everywhere to find the .gemrc file specification but I haven't succeed. Does
I'm trying to mmap a file, and it seems to succeed but when I
i try to send some data to my php file from ajax , but
I was trying to parse mathml document using JScience but was not succeed. Following
I am trying to upload file to google sites and am obtaining the stream
I'm using Python's unittest library and all the tests succeed, but I still get
I can't get the primeFaces <p:filedownload work. While I succeed in downloading the file
I succeed to display the menu (ContextMenu AS3 class) but associated events (ContextMenuEvent.MENU_SELECT and
Someone is FTPing a file of size 10Mb to folder on a linux server.

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.