Error with HTTPRequest and DocIO



我正试图使用Syncfusion示例中的以下代码在Xamarin Forms PCL中打开Word文档:

using System.Net;
using System;
using System.IO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
public void CreateWordDoc()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://valufy.file.core.windows.net/valufyfiles/ValufyReportWordTemplate.docx");
HttpWebResponse response = await (HttpWebResponse)request.GetResponse(); <----- FIRST ERROR HERE
Stream stream = response.GetResponseStream();
//Converts it to byte array
byte[] buffer = ReadFully(stream, 32768);
//Stores bytes into the memory stream.
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
ms.Seek(0, SeekOrigin.Begin);
stream.Close();
//Creates a new document.
WordDocument document = new WordDocument();
//Opens the template document from the MemoryStream.
document.Open(ms, FormatType.Doc);
//Saves and closes the document
document.Save("Sample.docx", FormatType.Docx); <---- SECOND ERROR HERE
document.Close();
}

我得到以下错误:

  1. ErrorCS1061"HttpWebRequest"不包含"GetResponse"的定义,并且没有接受第一个无法找到类型为"HttpWebRequest"的参数(是否缺少使用指令或程序集参考?)ValufyC:\Users\sresun\OneDrive\Projects\2Valfy\2Valfy_2Valufy\Data\Export.cs20Active
  2. ErrorCS1503参数1:无法从"string"转换为"System"。IO.Stream'ValufyC:\Users\sresun\OneDrive\Projects\2Valfy\2Valfy_2Valufy\Data\Export.cs48Active

任何建议都会有所帮助。GetResponse()方法是HttpWebRequest类的一个公共方法,我不知道它出错的原因。

错误1:

"HttpWebRequest"不包含"GetResponse"的定义,也找不到接受"HttpWebRequest"类型的第一个参数的扩展方法"GetResponse"(是否缺少using指令或程序集引用?)

建议:在DocIO PCL版本中不支持GetResponse()。因此,我们建议您使用GetResponseAsync()从HttpWebRequest获取响应。

注意:在尝试使用给定的URI字符串获取响应时,由于URI字符串无效,我们遇到了一些错误。因此,请使用适当的URI字符串依次创建web请求和响应。

我们怀疑此错误与DocIO可移植库无关。

错误2:

参数1:无法从"string"转换为"System"。IO.Stream’

建议:在Xamarin平台中,Essential DocIO不提供任何公共的API来直接在保存方法中指定文件名。这就是上述问题的原因("无法从‘string’转换为‘System.IO.Stream’)

或者,您可以使用"Save"方法和"Stream"重载来保存Word文档。

请参阅我们的UG文档,了解更多关于在Xamarin平台中使用DocIO可移植库加载和保存Word文档的信息。

最新更新