我正在一个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附件Guid,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)行之后,我看到了一个不同的值

请提出如何解决此问题的建议。我真的很感谢您的帮助。

最佳答案

这是因为您在这里从发布的文件中读取了数据:

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


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

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

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

关于c# - 系统异常-输入流不在Kentico中的开始位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29045020/

10-10 13:03