Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi , Please, anyone know how I can capture what has already been sent when sending data? I hope that later calculate upload speed, the length of time remaining and so, here is my code, or know of a better way? thank you very much :

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Collections.Specialized;
using System.Threading;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace upload_WithDetails
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);


        public void UploadFilesToRemoteUrl(string url, string file, string logpath, NameValueCollection nvc)
        {
            FileInfo f = new FileInfo("FilePath");

            Int64 fileSize = f.Length;
            long length = 0;
            string boundary = "----------------------------" +
            DateTime.Now.Ticks.ToString("x");


            HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
            boundary;
            httpWebRequest2.Method = "POST";
            httpWebRequest2.KeepAlive = true;
            httpWebRequest2.Credentials =
            System.Net.CredentialCache.DefaultCredentials;



            Stream memStream = new System.IO.MemoryStream();

            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
            boundary + "\r\n");
            string formdataTemplate = "\r\n--" +/* boundary +*/ "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

            foreach (string key in nvc.Keys)
            {
                string formitem = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }


            memStream.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

            string header = string.Format(headerTemplate, "form_upload", file);

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);


            memStream.Write(headerbytes, 0, headerbytes.Length);


            FileStream fileStream = new FileStream(file, FileMode.Open,
            FileAccess.Read);
            byte[] buffer = new byte[2048];

            int bytesRead = 0;

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

            
            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            
            fileStream.Close();

            httpWebRequest2.ContentLength = memStream.Length;

            Stream requestStream = httpWebRequest2.GetRequestStream();

            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];

            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            
            memStream.Close();

            requestStream.Write(tempBuffer, 0, tempBuffer.Length);            

            requestStream.Close();


            WebResponse webResponse2 = httpWebRequest2.GetResponse();

            Stream stream2 = webResponse2.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);

            webResponse2.Close();
            httpWebRequest2 = null;
            webResponse2 = null;


        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        NameValueCollection myCol = new NameValueCollection();
        string file;

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            myCol.Add("FileName", "1");
            file = "FilePath";            UploadFilesToRemoteUrl("http://up.uloz.to/ul/upload.cgi?tmp_sid=55fb51479a2ab312909cb47756ed3795&user_id=277895&host=uloz.to", file, "FolderPath", myCol);
            

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
        {
            
        }

    }
}
Posted
Updated 23-Jan-10 4:56am
v2

1 solution

What is your problem?
Are you asking on how to calculate the percentage of upload?

it should be readbytes / fileLength

And by the way, you are writing the whole data in the Request stream once.
requestStream.Write(tempBuffer, 0, tempBuffer.Length);

So the total request body will be sent once for you. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900