系统异常 - 输入流不在Kentico的起始位置

时间:2015-03-14 03:12:06

标签: c# .net kentico

我正在开发一个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.DocumentEngine.AttachmentInfo.LoadDataFromStream()   在CMS.DocumentEngine.AttachmentInfo.get_AttachmentBinary()   在CMS.DocumentEngine.AttachmentInfo..ctor(HttpPostedFile postedFile,Int32 documentId,Guid attachmentGuid)   在CMS.DocumentEngine.DocumentHelper.AddAttachment(TreeNode节点,字符串guidColumnName,Guid attachmentGuid,Guid groupGuid,目标文件,TreeProvider树,Int32宽度,Int32高度,Int32 maxSideSize)   在CMSModules_Content_Controls_Attachments_DirectFileUploader_DirectFileUploaderControl.HandleAttachmentUpload(Boolean fieldAttachment)

当我尝试在此行 System.Net.HttpWebResponse Response2 = (System.Net.HttpWebResponse)myReq.GetResponse() 上创建新回复时,它会给我这个错误。

我已经检查过,最初ucFileUpload.PostedFile.InputStream.Position为0但是在行ucFileUpload.PostedFile.InputStream.Read(myBytes,0,myBytes.Length)之后我看到了不同的值

请建议我如何解决这个问题。我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

这是因为您从此处发布的文件中读取数据:

//Read the file into the byte array.
httpPostedFile.InputStream.Read(myBytes, 0, myBytes.Length);

然后,在某处使用此方法之后,Kentico也会尝试从同一引用httpPostedFile中读取帖子数据。

使用其他人稍后可能会使用的输入流时的一个好习惯是将流的读取指针设置回开头,即:

httpPostedFile.InputStream.Seek(0, System.IO.SeekOrigin.Begin)