最新写一段代码,实现功能
1. 把上传文档转换成byte[]传会给调用方法;
2. 同时返回out xmldocument 对象;
如下:
protected Byte[] GetFile(FileUpload FileUploadFile,out XmlDocument xmlDoc)
{
string strFilePathName = FileUploadFile.PostedFile.FileName.Trim();
string strFileName = Path.GetFileName(strFilePathName);
int FileLength = FileUploadFile.PostedFile.ContentLength;
Byte[] FileByteArray = new Byte[FileLength];
Stream StreamObject = FileUploadFile.PostedFile.InputStream;
StreamObject.Read(FileByteArray, 0, FileLength);
//Out a xml document object
XmlDocument xmlDoctmp = new XmlDocument();
xmlDoctmp.Load(StreamObject);
xmlDoc = xmlDoctmp;
xmlDoctmp = null;
return FileByteArray;
}
编译抛出Exception:
....Root element is missing.....
原因&解决方案:
StreamObject.Read类似游标功能,在StreamObject.Read(FileByteArray, 0, FileLength);该游标已经从文件开始读到文件结束,
所以,必须重新设置游标位置,代码更改如下:
protected Byte[] GetFile(FileUpload FileUploadFile,out XmlDocument xmlDoc)
{
string strFilePathName = FileUploadFile.PostedFile.FileName.Trim();
string strFileName = Path.GetFileName(strFilePathName);
int FileLength = FileUploadFile.PostedFile.ContentLength;
Byte[] FileByteArray = new Byte[FileLength];
Stream StreamObject = FileUploadFile.PostedFile.InputStream;
StreamObject.Read(FileByteArray, 0, FileLength);
//Out a xml document object
XmlDocument xmlDoctmp = new XmlDocument();
StreamObject.Position = 0;
xmlDoctmp.Load(StreamObject);
xmlDoc = xmlDoctmp;
xmlDoctmp = null;
return FileByteArray;
}
编译通过