比如上传一个稍大一点的文件,可以在请求上传文件的时候,传过来一点就存入一点吗?问题比较幼稚,关键在于自己对HTTP协议认识的不清楚。

解决方案 »

  1.   

    断点续传
    断点续传

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections.Specialized;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    namespace CompleteClient
    {
        /// <summary>
        /// 文件发送工作类
        /// </summary>
        class PostFile
        {
            /// <summary>
            /// 通过post发送指定文件的指定字段到指定的uri上
            /// </summary>
            /// <param name="uploadfile">上传文件路径</param>
            /// <param name="url">上传的到的URi位置</param>
            /// <param name="offset">当前偏移量</param>
            /// <param name="size">需要发送的块大小</param>
            /// <param name="fileFormName">服务器端"GET"取得的文件名</param>
            /// <param name="contenttype">文件类型(保留用)</param>
            /// <param name="querystring">GET数组(供服务器用GET取得一些信息)</param>
            /// <param name="cookies">本地cookies(保留用)</param>
            /// <returns>uri的response的内容以string的形式返回</returns>
            public string UploadFileEx(string uploadfile, string url, long offset, long size,
                                       string fileFormName, string contenttype,
                                       NameValueCollection querystring, CookieContainer cookies)
            {
                if ((fileFormName == null) ||
                (fileFormName.Length == 0))
                {
                    fileFormName = "file";
                }            if ((contenttype == null) ||
                (contenttype.Length == 0))
                {
                    contenttype = "application/octet-stream";
                }            string postdata;
                postdata = "?";
                if (querystring != null)
                {
                    foreach (string key in querystring.Keys)
                    {
                        postdata += key + "=" + querystring.Get(key) + "&";
                    }
                }
                Uri uri = new Uri(url + postdata);            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
                webrequest.CookieContainer = cookies;
                webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
                webrequest.Method = "POST";            // 构造一个post请求的http头
                StringBuilder sb = new StringBuilder();
                sb.Append("--");
                sb.Append(boundary);
                sb.Append("\r\n");
                sb.Append("Content-Disposition: form-data; name=\"");
                sb.Append(fileFormName);
                sb.Append("\"; filename=\"");
                sb.Append(Path.GetFileName(uploadfile));
                sb.Append("\"");
                sb.Append("\r\n");
                sb.Append("Content-Type: ");
                sb.Append(contenttype);
                sb.Append("\r\n");
                sb.Append("\r\n");            string postHeader = sb.ToString();
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);            // Build the trailing boundary string as a byte array
                // ensuring the boundary appears on a line by itself
                byte[] boundaryBytes =
                Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");            FileStream fileStream = new FileStream(uploadfile,
                                FileMode.Open, FileAccess.Read);
                long length = postHeaderBytes.Length + (long)size +
                                           boundaryBytes.Length;
                webrequest.ContentLength = length;            Stream requestStream = webrequest.GetRequestStream();
                // 写入post头
                requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);            // 写入文件内容
                byte[] buffer = new Byte[size];
                fileStream.Seek(offset, SeekOrigin.Current);
                fileStream.Read(buffer, 0, buffer.Length);            requestStream.Write(buffer, 0, buffer.Length);            // 写入post请求的尾
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                //读取服务器的反馈消息
                WebResponse responce = webrequest.GetResponse();
                Stream s = responce.GetResponseStream();
                StreamReader sr = new StreamReader(s);            return sr.ReadToEnd();
            }
        }
    }
      

  2.   

    当然是分段处理的...事实上在请求之前先发送HTTP头,然后才发送请求,最后才发送form-data...而form-data又要由缓冲区分块存储...想认识HTTP协议很简单,试着自己写个简单的HTTP服务器就都清楚了...
      

  3.   

    数据缓存在系统里,就算你在asp.net端不接受,也是缓存在.net为其开的缓冲区的(这并不等于tcp的窗口缓冲区)。我不知道你说的“在全部到达服务器之前就进行处理”具体是指什么处理。但是你想打断上传,甚至终止上传,而给客户端返回值,办不到!除非你不使用asp.net,使用其它自定义的、已经为中间各个控制过程预留了接口的上传控件。
      

  4.   


    这部重要,这只是涉及性能测试问题。关键是简单原始的html上传以及asp.net处理它的机制,没有可能中断上传而开始下载。
      

  5.   

    嗯,这样。ASP.NET在最早可以处理请求时(Application_BeginRequest),HTTP请求所发送的文件就已经上传完了吗?
      

  6.   

    用FTP吧,Http不是用来上传大数据量的
    建议你用WCF的MTOM机制来传输
    http://www.dotblogs.com.tw/jeff-yeh/archive/2009/09/27/10798.aspx