我正在开发一个Kentico项目,我必须将MetaScan集成到Kentico中。我试图通过方法callMetaScan(CMSFileUpload.PostedFile)
发送一个HttpPosted文件,我有以下callMetaScan
方法
private void callMetaScan(System.Web.HttpPostedFile httpPostedFile)
{
string strDate = string.Format("{0} GMT", DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss"));
System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(myURI);
myReq.Date = Convert.ToDateTime(strDate);
myReq.Method = "POST";
byte[] myBytes = new byte[httpPostedFile.InputStream.Length];
//Read the file into the byte array.
httpPostedFile.InputStream.Read(myBytes, 0, myBytes.Length);
myReq.ContentLength = myBytes.Length;
myReq.ContentType = httpPostedFile.ContentType;
System.IO.Stream PostData = (System.IO.Stream)myReq.GetRequestStream();
PostData.Write(myBytes, 0, myBytes.Length);
PostData.Close();
System.Net.HttpWebResponse Response2 = (System.Net.HttpWebResponse)myReq.GetResponse();
//throw new Exception(GetString("1"));
System.IO.StreamReader SR = default(System.IO.StreamReader);
SR = new System.IO.StreamReader(Response2.GetResponseStream());
string rslt = SR.ReadToEnd();
SR.Close();
System.Xml.XmlDocument xmlRslt = new System.Xml.XmlDocument();
xmlRslt.LoadXml(rslt);
//if no final_scan_result, then save a copy of the xml and email us.
if (xmlRslt.SelectSingleNode(pathstring) != null)
{
int finalScanResultCode = Int32.Parse(xmlRslt.SelectSingleNode(pathstring).InnerText);
}
else
{
//Send Email about the XML
Response.Write(“Unable to scan file.”);
}
}
我收到以下错误消息
[AttachmentInfo.EnsureBinaryData]:输入流不在起始位置。异常类型:System.Exception堆栈跟踪:在CMS的CMS.DocumentEngine.AttachmentInfo.LoadDataFromStream()CMS.DocumentEngine.AttachmentInf.get_AttachmentBinary()CMS上的CMS.DocumentEngine.AttachmentInfo ...(HttpPostedFile postedFile,Int32 documentId,Guid attachmentGuid) CMSModules_Content_Controls_Attachments_DirectFileUploader_DirectFileUploaderControl.HandleAttachmentUpload(Boolean fieldAttachment)中的.DocumentEngine.DocumentHelper.AddAttachment(TreeNode节点,字符串guidColumnName,Guid attachmentGuid,Guid groupGuid,目标文件,TreeProvider树,Int32宽度,Int32高度,Int32 maxSideSize)
当我试图在这行
System.Net.HttpWebResponse Response2 = (System.Net.HttpWebResponse)myReq.GetResponse()
上创建一个新的响应时,它给了我这个错误。
我已经检查过,最初ucFileUpload.PostedFile.InputStream.Position为0但是在行ucFileUpload.PostedFile.InputStream.Read(myBytes,0,myBytes.Length)之后我看到了不同的值
请建议我如何解决这个问题。我真的很感激任何帮助。
这是因为你在这里读取了发布文件中的数据:
//Read the file into the byte array.
httpPostedFile.InputStream.Read(myBytes, 0, myBytes.Length);
然后,在这个方法之后,Kentico也尝试从同一参考httpPostedFile
的帖子数据中读取。
使用其他人稍后可能使用的输入流时的一个好习惯是将流的读指针设置回到开头,即:
httpPostedFile.InputStream.Seek(0, System.IO.SeekOrigin.Begin)