Hello Aspose Community
i tried to open a word (doc) document
SPFile templateFile = web.GetFile(TemplateName);
Stream binFile = templateFile.OpenBinaryStream();
Document dstDoc = new Document(binFile, null); //<- throws the failure:
System.InvalidCastException: object of type "System.String" cann't convert into type "System.Byte[]" .
the code works for some word docs but for some others it crashes, i attached a doc which crashes
any ideas?
Thanks for your request. I cannot reproduce the problem on my side using the latest version of Aspose.Words (9.4.0). I use the following code for testing:
// Get bytes
byte[] docBytes = File.ReadAllBytes("canntOpen.doc");
// Create memorystream
using(MemoryStream docStream = new MemoryStream(docBytes))
{
// Create document from stream
Document doc = new Document(docStream);
doc.Save("out.pdf");
}
Thank you for additional information. Please try using code like the following to open a document from url:
string url = @"http://localhost/mysite/mydocument.doc";
// Prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/msword";
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
// Execute the request
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
// We will read data via the response stream
Stream resStream = response.GetResponseStream();
// Write content into the MemoryStream BinaryReader resReader = new BinaryReader(resStream);
MemoryStream docStream = new MemoryStream(resReader.ReadBytes((int) response.ContentLength));
Document doc = new Document(docStream);
// Save document
doc.Save(@"out.doc");