How to upload multiple files using HttpWebRequest in c#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdunnabisk
    New Member
    • Jan 2008
    • 17

    How to upload multiple files using HttpWebRequest in c#?

    How to upload multiple files at a time using HttpWebRequest in c#?

    thanks
    Abdun Nabi Sk
  • GregoryPankov
    New Member
    • May 2008
    • 6

    #2
    Originally posted by abdunnabisk
    How to upload multiple files at a time using HttpWebRequest in c#?

    thanks
    Abdun Nabi Sk
    You can use WebClient.FileU pload method to send all files one by one to the server. Shortcoming: one file is sent in one request.

    You can use third party components (ex:Chilkat.Upl oad)

    Use can also write your own class, that based on HttpWebRequest. This class should implement RFC 1867 (www.ietf.org/rfc/rfc1867.txt). In this case you will need to build http request and send it to request stream.

    Comment

    • abdunnabisk
      New Member
      • Jan 2008
      • 17

      #3
      Thansks for your reply.I want to upload more than one file using httpwebrequest in a single transaction and not one by one.Can you provide me some codes on that.

      Thanks
      Abdun nabi sk

      Originally posted by GregoryPankov
      You can use WebClient.FileU pload method to send all files one by one to the server. Shortcoming: one file is sent in one request.

      You can use third party components (ex:Chilkat.Upl oad)

      Use can also write your own class, that based on HttpWebRequest. This class should implement RFC 1867 (www.ietf.org/rfc/rfc1867.txt). In this case you will need to build http request and send it to request stream.

      Comment

      • abdunnabisk
        New Member
        • Jan 2008
        • 17

        #4
        Finally I got this done...here is the code

        string boundary = "----------------" +
        DateTime.Now.Ti cks.ToString("x ");

        string url = ConfigurationMa nager.AppSettin gs["ServerAddr ess"];
        HttpWebRequest httpWebRequest = (HttpWebRequest )WebRequest.Cre ate(url);
        httpWebRequest. ContentType = fileContentType +";+ boundary=" + boundary;
        httpWebRequest. Method = "POST";
        httpWebRequest. KeepAlive = true;
        httpWebRequest. Timeout = 600000;
        httpWebRequest. ReadWriteTimeou t = 600000;
        //httpWebRequest. AllowWriteStrea mBuffering = true;
        httpWebRequest. Credentials=Sys tem.Net.Credent ialCache.Defaul tCredentials;

        Stream memStream = new System.IO.Memor yStream();

        byte[] boundarybytes = System.Text.Enc oding.ASCII.Get Bytes("\r\n--" + boundary + "\r\n");
        memStream.Write (boundarybytes, 0, boundarybytes.L ength);

        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\ "\r\n Content-Type:" + defaultContentT ype + "\r\n\r\n";

        for(int i=0;i<files.Len gth;i++)
        {
        string header = string.Format(h eaderTemplate," file"+i,new FileInfo(files[i]).Name);
        byte[] headerbytes = System.Text.Enc oding.UTF8.GetB ytes(header);
        memStream.Write (headerbytes,0, headerbytes.Len gth);
        FileStream fileStream = new FileStream(file s[i], FileMode.Open,
        FileAccess.Read );
        byte[] buffer = new byte[1024];
        int bytesRead = 0;

        while ( (bytesRead = fileStream.Read (buffer, 0, buffer.Length)) != 0 )
        {
        memStream.Write (buffer, 0, bytesRead);
        }

        memStream.Write (boundarybytes, 0,boundarybytes .Length);
        fileStream.Clos e();
        }

        httpWebRequest. ContentLength = memStream.Lengt h;

        Stream requestStream = httpWebRequest. GetRequestStrea m();
        memStream.Posit ion = 0;
        byte[] tempBuffer = new byte[memStream.Lengt h];
        memStream.Read( tempBuffer, 0, tempBuffer.Leng th);
        memStream.Close ();
        requestStream.W rite(tempBuffer , 0, tempBuffer.Leng th);
        requestStream.C lose();

        try
        {
        WebResponse webResponse = httpWebRequest. GetResponse();
        Stream stream = webResponse.Get ResponseStream( );
        StreamReader reader = new StreamReader(st ream);
        Console.WriteLi ne(reader.ReadT oEnd());
        }
        catch (Exception ex)
        {
        Console.WriteLi ne(ex.ToString( ));
        }
        finally
        {
        }
        httpWebRequest = null;

        Originally posted by abdunnabisk
        Thansks for your reply.I want to upload more than one file using httpwebrequest in a single transaction and not one by one.Can you provide me some codes on that.

        Thanks
        Abdun nabi sk

        Comment

        • azmanamutalib
          New Member
          • Oct 2009
          • 1

          #5
          How to receive multiple files?

          That's great! But how do we actually receive the multiple files? Do you have any code sample for the receiving URL?

          Thanks.

          Regards,
          Azman

          Comment

          Working...