检查上传进度 [英] check upload progress

查看:56
本文介绍了检查上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,如何从该代码中获取状态已发送数据的信息?发送到http服务器的"post"方法正在工作,但是我仍然无法在运行中获得状态.有人写信给我,我说的只是"readbytes/filelenght",但是实际上如何读取字节呢?我几乎尝试了所有方法,但仍然没有尝试.如果我没记错的话,服务器将通过"requestStream.Write(tempBuffer,0,tempBuffer.Length)"写入数据,并且确切地说,requestStreamu我不会获取任何数据.请指教.

Hi, how can I get the state has sent data from this code? Sending to http server "post" method is working, but I still can not gain status in the running. Someone wrote me that just "readbytes / filelenght" It is obvious, but how do actually read bytes? I tried almost all ways and still nothing. If I remember correctly, the server will write data by "requestStream.Write (tempBuffer, 0, tempBuffer.Length)," and precisely requestStreamu I do not go getting any data. Please advice.

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)
        {
            
        }

    }
}

推荐答案

我想我早在这里已经回答了相同的问题.


您遇到的问题实际上是您一次发送总流.

您需要分批发送.该服务器是否支持TCP文件传输?
I think I have already answered the same question long ago here.


The problem with you is actually you are sending the total stream at a time.

You need to send in parts. Does this server supports TCP file transfer ?


这篇关于检查上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆