Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Bytes to be written to the stream exceed the Content-Length bytes size specified.

Bytes to be written to the stream exceed the Content-Length bytes size specified.

Scheduled Pinned Locked Moved C#
helpsysadmindebugging
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nasambur
    wrote on last edited by
    #1

    Hi.. I'm trying to upload a image file to the server using HttpWebRequest/HttpWebResponse. so i form a Postdata as and storing in a StringBuilder object (sbFormData)..

    Content-Type: multipart/form-data; boundary=AaB03x
    
       --AaB03x
       Content-Disposition: form-data; name="submit-name"
    
       Larry
       --AaB03x
       Content-Disposition: form-data; name="files"
       Content-Type: multipart/mixed; boundary=BbC04y
    
       --BbC04y
       Content-Disposition: file; filename="file1.txt"
       Content-Type: text/plain
    
       ... contents of file1.txt ...
       --BbC04y
       Content-Disposition: file; filename="file2.gif"
       Content-Type: image/gif
       Content-Transfer-Encoding: binary
    
       ...contents of file2.gif...
       --BbC04y--
       --AaB03x--
    

    while adding the image content i read byte by byte and then adding to the sbFormData. But when i see the sbFormdata's length its not showing exact length. That is not adding the image content's length. Then setting the HttpWebRequest.Length to sbFormdata.Length.. httpWebRequest.ContentLength = sbFormData.Length; after when i try to write the sbFormData to the httpWebRequest.GetRequestStream(), i get Error: "Bytes to be written to the stream exceed the Content-Length bytes size specified."

    try
    {
      StreamWriter s = new StreamWriter(httpWebRequest.GetRequestStream());
      s.Write(sbFormData.ToString());        
      s.Close();
    }
    catch (Exception e)
    {
      Debug.Print("Error in sending data " + e.Message);
    }
    

    Please help me.. regards, nas

    G 1 Reply Last reply
    0
    • N nasambur

      Hi.. I'm trying to upload a image file to the server using HttpWebRequest/HttpWebResponse. so i form a Postdata as and storing in a StringBuilder object (sbFormData)..

      Content-Type: multipart/form-data; boundary=AaB03x
      
         --AaB03x
         Content-Disposition: form-data; name="submit-name"
      
         Larry
         --AaB03x
         Content-Disposition: form-data; name="files"
         Content-Type: multipart/mixed; boundary=BbC04y
      
         --BbC04y
         Content-Disposition: file; filename="file1.txt"
         Content-Type: text/plain
      
         ... contents of file1.txt ...
         --BbC04y
         Content-Disposition: file; filename="file2.gif"
         Content-Type: image/gif
         Content-Transfer-Encoding: binary
      
         ...contents of file2.gif...
         --BbC04y--
         --AaB03x--
      

      while adding the image content i read byte by byte and then adding to the sbFormData. But when i see the sbFormdata's length its not showing exact length. That is not adding the image content's length. Then setting the HttpWebRequest.Length to sbFormdata.Length.. httpWebRequest.ContentLength = sbFormData.Length; after when i try to write the sbFormData to the httpWebRequest.GetRequestStream(), i get Error: "Bytes to be written to the stream exceed the Content-Length bytes size specified."

      try
      {
        StreamWriter s = new StreamWriter(httpWebRequest.GetRequestStream());
        s.Write(sbFormData.ToString());        
        s.Close();
      }
      catch (Exception e)
      {
        Debug.Print("Error in sending data " + e.Message);
      }
      

      Please help me.. regards, nas

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You are setting the content length to the number of characters in the string, but that is not the same as the number of bytes that the string uses when encoded. You have to encode the string first to get the size of the encoded data.

      --- single minded; short sighted; long gone;

      N 1 Reply Last reply
      0
      • G Guffa

        You are setting the content length to the number of characters in the string, but that is not the same as the number of bytes that the string uses when encoded. You have to encode the string first to get the size of the encoded data.

        --- single minded; short sighted; long gone;

        N Offline
        N Offline
        nasambur
        wrote on last edited by
        #3

        Thanx..:rose: I got it..

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups