后台提交页面的超难问题.求高手解决

页面一:用于发送请求并接收返回数据(此处需返回服务器中存储的EXCEL文件
         string  mScriptName  =   " Default.aspx " ;
        
string  mServerName  =   " Server.aspx " ;
        
string  mHttpUrl  =   " http:// "   +  Request.ServerVariables[ " HTTP_HOST " +  Request.ServerVariables[ " SCRIPT_NAME " ];
         mHttpUrl 
=  mHttpUrl.Substring( 0 , mHttpUrl.Length  -  mScriptName.Length);
        
string  mServerUrl  =  mHttpUrl  +  mServerName;
        
        HttpWebRequest request 
=  (HttpWebRequest)WebRequest.Create(mServerUrl);
        request.UserAgent 
=   " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon) " ;
        request.Method 
=   " POST " ;
        request.ContentType 
=   " application/x-www-form-urlencoded " ;
        request.Timeout 
=   300000 ;

        
        WebResponse resp 
=  request.GetResponse();
        StreamReader sr 
=   new  StreamReader(resp.GetResponseStream(), System.Text.Encoding.Default);

        
byte [] cc  =  System.Text.Encoding.Default.GetBytes(sr.ReadToEnd());

        MsgObj.MsgVariant(cc);

        MsgObj.MsgFileSave( Server.MapPath(
" . " +   " /bbb.xls " );
        sr.Close();[
/ code]
这是页面二,用来提供请求数据(从数据库中读取存储的文件提供给发送请求的页面.)
    public   byte [] mFileBody;
    
protected   void  Page_Load( object  sender, EventArgs e)
    
{
        MsgServer MsgObj 
= new MsgServer();
        
        
string connstring = "server=.;database=ActivexDB;uid=sa;pwd=sa";
        SqlConnection conn 
= new SqlConnection(connstring);
        conn.Open();
        
string Sqlstr = "Select * From OCX_Test where FileName ='First_TestFile'";
        SqlCommand cmd 
= new SqlCommand(Sqlstr, conn);
        SqlDataReader reader 
= cmd.ExecuteReader();
        
if (reader.Read())
        
{
            mFileBody 
= reader.GetSqlBinary(2).Value;          
            MsgObj.MsgFileBody(mFileBody);
            
//string aa = Server.MapPath(".") + "/aaaa.xls";   //在这里直接将读出的EXCEL文件保存为文件是正常的.
            
//MsgObj.MsgFileSave(aa);
        }

        reader.Close();
        conn.Close();
        Response.BinaryWrite(MsgObj.MsgVariant());
       }
下面是MsgObj类的方法:
  public  MsgServer()
    
{
        
this.FMsgText = "";
        
this.FError = "";
        
this.FVersion = this.VERSION;
    }


    
private   string  ByteToString( byte [] vByte)
    
{
        
return Encoding.Default.GetString(vByte);
    }


    
private   string  FormatHead( string  vString)
    
{
        
if (vString.Length > 0x10)
        
{
            
return vString.Substring(00x10);
        }

        
for (int i = vString.Length + 1; i < 0x11; i++)
        
{
            vString 
= vString + " ";
        }

        
return vString;
    }


    
public   int  GetFieldCount()
    
{
        
int num = 0;
        
int num2 = 0;
        
for (num = this.FMsgText.IndexOf(" ", (int)(num + 1)); num != -1; num = this.FMsgText.IndexOf(" ", (int)(num + 1)))
        
{
            num2
++;
        }

        
return num2;
    }


    
public   string  GetFieldName( int  Index)
    
{
        
int startIndex = 0;
        
int num2 = 0;
        
int index = 0;
        
int length = 0;
        
string str = "";
        
string str3 = "";
        
while ((startIndex != -1&& (num2 < Index))
        
{
            startIndex 
= this.FMsgText.IndexOf(" ", (int)(startIndex + 1));
            
if (startIndex != -1)
            
{
                num2
++;
            }

        }

        index 
= this.FMsgText.IndexOf(" ", (int)(startIndex + 1));
        
if ((startIndex != -1&& (index != -1))
        
{
            
if (startIndex == 0)
            
{
                str 
= this.FMsgText.Substring(startIndex, index - startIndex);
            }

            
else
            
{
                str 
= this.FMsgText.Substring(startIndex + 2, index - (startIndex + 2));
            }

            length 
= str.IndexOf("="0);
            
if (length != -1)
            
{
                str3 
= str.Substring(0, length);
            }

        }

        
return str3;
    }


    
public   string  GetFieldText()
    
{
        
return this.FMsgText.ToString();
    }


    
public   string  GetFieldValue( int  Index)
    
{
        
int startIndex = 0;
        
int num2 = 0;
        
int index = 0;
        
int num4 = 0;
        
string str = "";
        
string str2 = "";
        
string str3 = "";
        
while ((startIndex != -1&& (num2 < Index))
        
{
            startIndex 
= this.FMsgText.IndexOf(" ", (int)(startIndex + 1));
            
if (startIndex != -1)
            
{
                num2
++;
            }

        }

        index 
= this.FMsgText.IndexOf(" ", (int)(startIndex + 1));
        
if ((startIndex != -1&& (index != -1))
        
{
            
if (startIndex == 0)
            
{
                str 
= this.FMsgText.Substring(startIndex, index - startIndex);
            }

            
else
            
{
                str 
= this.FMsgText.Substring(startIndex + 2, index - (startIndex + 2));
            }

            num4 
= str.IndexOf("="0);
            
if (num4 != -1)
            
{
                str2 
= str.Substring(num4 + 1, str.Length - (num4 + 1));
                str3 
= this.DecodeBase64(str2);
            }

        }

        
return str3;
    }


    
public   string  GetMsgByName( string  FieldName)
    
{
        
int startIndex = 0;
        
int index = 0;
        
string str = "";
        
string str3 = FieldName + "=";
        startIndex 
= this.FMsgText.IndexOf(str3);
        
if (startIndex != -1)
        
{
            index 
= this.FMsgText.IndexOf(" ", (int)(startIndex + 1));
            startIndex 
+= str3.Length;
            
if (index != -1)
            
{
                
string str2 = this.FMsgText.Substring(startIndex, index - startIndex);
                
return this.DecodeBase64(str2);
            }

            
return str;
        }

        
return str;
    }


    
public   bool  MakeDirectory( string  FilePath)
    
{
        
if (!Directory.Exists(FilePath))
        
{
            Directory.CreateDirectory(FilePath);
        }

        
return Directory.Exists(FilePath);
    }


    
public   string  MsgError()
    
{
        
return this.FError;
    }


    
public   void  MsgError( string  Value)
    
{
        
this.FError = Value;
    }


    
public   void  MsgErrorClear()
    
{
        
this.FError = "";
    }


    
public   byte [] MsgFileBody()
    
{
        
return this.FMsgFile;
    }


    
public   void  MsgFileBody( byte [] Value)
    
{
        
this.FMsgFile = Value;
        
this.FFileSize = this.FMsgFile.Length;
    }


    
public   void  MsgFileClear()
    
{
        
this.FFileSize = 0;
        
this.FMsgFile = null;
    }


    
public   bool  MsgFileLoad( string  FileName)
    
{
        
try
        
{
            FileStream stream 
= new FileStream(FileName, FileMode.Open);
            
this.FFileSize = (int)stream.Length;
            
this.FMsgFile = new byte[this.FFileSize];
            stream.Read(
this.FMsgFile, 0this.FFileSize);
            stream.Close();
            
return true;
        }

        
catch (Exception exception)
        
{
            
this.FError = this.FError + exception.ToString();
            
return false;
        }

    }


    
public   bool  MsgFileSave( string  FileName)
    
{
        
try
        
{
            FileStream stream 
= new FileStream(FileName, FileMode.Create);
            stream.Write(
this.FMsgFile, 0this.FFileSize);
            stream.Close();
            
return true;
        }

        
catch (Exception exception)
        
{
            
this.FError = this.FError + exception.ToString();
            
return false;
        }

    }


    
public   int  MsgFileSize()
    
{
        
return this.FFileSize;
    }


    
public   void  MsgFileSize( int  value)
    
{
        
this.FFileSize = value;
    }


    
public   string  MsgTextBody()
    
{
        
return this.FMsgText;
    }


    
public   void  MsgTextBody( string  Value)
    
{
        
this.FMsgText = Value;
    }


    
public   void  MsgTextClear()
    
{
        
this.FMsgText = "";
    }


    
public   bool  MsgToStream()
    
{
        
int count = 0x40;
        
int length = 0;
        
int num3 = 0;
        
int fFileSize = 0;
        
int num5 = 0;
        
try
        
{
            num5 
= 0;
            length 
= this.FMsgText.Length;
            num3 
= this.FError.Length;
            fFileSize 
= this.FFileSize;
            MemoryStream stream 
= new MemoryStream(((count + length) + num3) + fFileSize);
            
string vString = this.FormatHead(this.FVersion) + this.FormatHead(length.ToString()) + this.FormatHead(num3.ToString()) + this.FormatHead(fFileSize.ToString());
            stream.Write(
this.StringToByte(vString), 0, count);
            num5 
+= count;
            
if (length > 0)
            
{
                stream.Write(
this.StringToByte(this.FMsgText), 0, length);
            }

            num5 
+= length;
            
if (num3 > 0)
            
{
                stream.Write(
this.StringToByte(this.FError), 0, num3);
            }

            num5 
+= num3;
            
if (fFileSize > 0)
            
{
                stream.Write(
this.FMsgFile, 0, fFileSize);
            }

            num5 
+= fFileSize;
            stream.Close();
            
this.FStream = stream.ToArray();
            
return true;
        }

        
catch (Exception exception)
        
{
            
this.FError = this.FError + exception.ToString();
            
return false;
        }

    }


    
public   byte [] MsgVariant()
    
{
        
if (this.TableBase64.IndexOf(this.TableBase60.Substring(150x26)) == -1)
        
{
            
this.MsgTextClear();
            
this.MsgFileClear();
        }

        
this.MsgToStream();
        
return this.FStream;
    }


    
public   void  MsgVariant( byte [] mStream)
    
{
        
this.FStream = mStream;
        
if (this.FError == "")
        
{
            
this.StreamToMsg();
        }

    }


    
public   string  MsgVersion()
    
{
        
return this.FVersion;
    }


    
public   bool  SavePackage( string  FileName)
    
{
        
try
        
{
            FileStream stream 
= new FileStream(FileName, FileMode.Create);
            stream.Write(
this.FStream, 0this.FStream.Length);
            stream.Close();
            
return true;
        }

        
catch (Exception exception)
        
{
            
this.FError = this.FError + exception.ToString();
            
return false;
        }

    }


    
public   void  SetMsgByName( string  FieldName,  string  FieldValue)
    
{
        
string str = "";
        
string str2 = "";
        
string str3 = "";
        
int length = 0;
        
int index = 0;
        
bool flag = false;
        
string str4 = FieldName.Trim() + "=";
        
string str5 = this.EncodeBase64(FieldValue);
        str 
= str4 + str5 + " ";
        length 
= this.FMsgText.IndexOf(str4);
        
if (length != -1)
        
{
            index 
= this.FMsgText.IndexOf(" ", (int)(length + 1));
            
if (index != -1)
            
{
                str2 
= this.FMsgText.Substring(0, length);
                str3 
= this.FMsgText.Substring(index + 2);
                flag 
= true;
            }

        }

        
if (flag)
        
{
            
this.FMsgText = str2 + str + str3;
        }

        
else
        
{
            
this.FMsgText = this.FMsgText + str;
        }

    }


    
private   bool  StreamToMsg()
    
{
        
string str = "";
        
int count = 0x40;
        
int num2 = 0;
        
int num3 = 0;
        
int num4 = 0;
        
int num5 = 0;
        
try
        
{
            MemoryStream stream 
= new MemoryStream(this.FStream);
            num5 
= 0;
            
byte[] buffer = new byte[count];
            stream.Read(buffer, 
0, count);
            str 
= this.ByteToString(buffer);
            
this.FVersion = str.Substring(00x10);
            num2 
= int.Parse(str.Substring(0x100x10).Trim());
            num3 
= int.Parse(str.Substring(0x200x10).Trim());
            num4 
= int.Parse(str.Substring(0x300x10).Trim());
            
this.FFileSize = num4;
            num5 
+= count;
            
if (num2 > 0)
            
{
                buffer 
= new byte[num2];
                stream.Read(buffer, 
0, num2);
                
this.FMsgText = this.ByteToString(buffer);
            }

            num5 
+= num2;
            
if (num3 > 0)
            
{
                buffer 
= new byte[num3];
                stream.Read(buffer, 
0, num3);
                
this.FError = this.ByteToString(buffer);
            }

            num5 
+= num3;
            
if (num4 > 0)
            
{
                
this.FMsgFile = new byte[num4];
               
                stream.Read(
this.FMsgFile, 0, num4);
                
string aaaaa = this.ByteToString(this.FMsgFile);
            }

            
return true;
        }

        
catch (Exception exception)
        
{
            
this.FError = this.FError + exception.ToString();
            
return false;
        }

    }


    
private   byte [] StringToByte( string  vString)
    
{
        
return Encoding.Default.GetBytes(vString);
    }

}


现在的问题是接收的数据写成文件后无法打开,提示格式错误.

你可能感兴趣的:(后台提交页面的超难问题.求高手解决)