Wu.Country@侠缘

勤学似春起之苗,不见其增,日有所长; 辍学如磨刀之石,不见其损,日所有亏!

导航

[导入]SunriseUpload.0.9.1的源码分析(四)

分析提交的数据,其中有一项基本的功能就是要从数据里取得表单数据,例如文本输入框等,虽然这可能都交给ASP.net的模块去做,但我们还是得自己处理一下,至少也应该提供一个函数来处理它。
SunriseUpload里用的是这样的一个函数来处理它的:
  private string AnalysePreloadedEntityBody(byte[] preloadedEntityBody, string name)
  {
   string val = string.Empty;
   string preloadedContent = Utils.GetContext().Request.ContentEncoding.GetString(preloadedEntityBody);
   if (preloadedContent.Length > 0)
   {
    int startIndex = ((preloadedContent.IndexOf(("name=\"" + name + "\"")) + 11) + name.Length);
    int endIndex = preloadedContent.IndexOf("\r\n", startIndex);
    val = preloadedContent.Substring(startIndex, (endIndex - startIndex));
   }
   return val;
  }
然而它会出一个小问题,就是如果我在一个文本输入域里输入了一个回车和换行,那么它将截断字符串,因此我也写了一下:
  private string AnalysePreLoadedData(byte[] m_preLoadedData, string m_fiedName)
  {
   string m_returnData  = string.Empty;
   string preloadedContent = Aidance.GetContext().Request.ContentEncoding.GetString(m_preLoadedData);
   if (preloadedContent.Length > 0)
   { 
    int startIndex = ((preloadedContent.IndexOf(("name=\"" + m_fiedName + "\"")) + 11) + m_fiedName.Length);
//    int endIndex = preloadedContent.IndexOf("\r\n", startIndex);
    int endIndex = preloadedContent.IndexOf(m_boundary,startIndex);
    m_returnData = preloadedContent.Substring(startIndex, (endIndex - startIndex));
   }
   return m_returnData;
  }
这里的m_boundary是前面已经处理过的数据分隔标识。当然我还是觉得这样用字符串来处理不是很高效,但又不知道应该怎样做。

另一个不太明白的就是:
int m_flieSize      = Convert.ToInt32(m_workerRequest.GetKnownRequestHeader(11));
我试了用循环来处理所有的m_workerRequest.GetKnownRequestHeader,最后还得到了URL及文件名,而11所得到的也不是决对的文件长度,只是很接近,而且差的数据是一个固定的,我猜这个差值应该是在表单里占用了。
但上传小文件(<48K)的,都将没有区别,它和上传所读取的数据是一致的。也就是说如果一个上传文件和表单数据可以在一次传输中完成,那么这个数据就等于读取数据。因此这里(m_workerRequest.GetKnownRequestHeader所得到的并不是上传文件的大小,但我们可以用它为粗略的判断一下文件的大小,因为毕竟当文件很大的时候(几百MB),表单里的一些数据可以忽略不计的。
最后说明的是,这里的index最多只能到38,在太就出错。。。。我不知道这个index到底是什么?

好了,不管了先,下面自己写这个RequestStream类。仿照原来的做法,类的参数,属性以及构造函数都没变(改了一下变量名,呵呵,用我喜欢的名字)。
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;

namespace WebbTest
{
 /// <summary>
 /// Summary description for RequestStream.
 /// </summary>
 public class RequestStream
 {
  #region Field
  private ArrayList m_contentBody;
  private FileStatus m_fileStatus;
  private FileStream m_fs;
  private ArrayList m_readBody;
  private ReadStatus m_readStatus;
  private string  m_originalFileName;
  #endregion

  public enum FileStatus : byte
  {
   // Fields
   Close = 1,
   Open = 0
  }

  public enum ReadStatus : byte
  {
   // Fields
   NoRead = 0,
   Read = 1
  }

  #region Propertis
  public ArrayList ContentBody
  {
   get { return this.m_contentBody;}
  }

  public FileStream FileStream
  {
   get { return this.m_fs; }
  }

  public FileStatus FStatus
  {
   get { return this.m_fileStatus; }
  }

  public string OriginalFileName
  {
   get { return this.m_originalFileName; }
  }

  public ArrayList ReadBody
  {
   get { return this.m_readBody; }
  }

  public ReadStatus RStatus
  {
   get { return this.m_readStatus; }
  }

  #endregion

  public RequestStream( byte[] preloadBytes,
        byte[] boundaryBytes,
        FileStream fileStream,
        FileStatus fileStatus,
        ReadStatus readStatus,
        string uploadFolder,
        bool writeToDisk,
        HttpContext context,
        string currFileName)
  {
   #region init variables   
   this.m_readBody   = new ArrayList();
   this.m_contentBody  = new ArrayList();
   //
   this.m_fs    = null;
   this.m_originalFileName = string.Empty;
   this.m_fileStatus  = FileStatus.Close;
   this.m_readStatus  = ReadStatus.NoRead;
   //
   this.m_fs    = fileStream;
   this.m_originalFileName = currFileName; 
   this.m_fileStatus  = fileStatus;
   this.m_readStatus  = readStatus;
   #endregion

  }
 }
}

TNND,这个函数太长了,没能写完。。。。。明天再写。。。


文章来源:http://computer.mblogger.cn/wucountry/posts/48515.aspx

posted on 2005-11-03 01:30  Wu.Country@侠缘  阅读(509)  评论(0编辑  收藏  举报