We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Error with HTTPRequest and DocIO

Thread ID:

Created:

Updated:

Platform:

Replies:

128718 Feb 7,2017 07:18 PM UTC Feb 22,2017 04:06 AM UTC Xamarin.Forms 5
loading
Tags: DocIO
Sree Sundaram
Asked On February 7, 2017 07:18 PM UTC

Hello. I am trying to open a Word Document in Xamarin Forms PCL using the following code from Syncfusion example:
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();
}
I am getting the following errors:
1. ErrorCS1061'HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)ValufyC:\Users\sreesun\OneDrive\Projects\2Valufy\2Valufy\_2Valufy\Data\Export.cs20Active
2. ErrorCS1503Argument 1: cannot convert from 'string' to 'System.IO.Stream'ValufyC:\Users\sreesun\OneDrive\Projects\2Valufy\2Valufy\_2Valufy\Data\Export.cs48Active


Sethumanikkam Yogendran [Syncfusion]
Replied On February 8, 2017 10:08 AM UTC

Hi Sree,

Thank you for contacting Syncfusion support.
 
Error 
Explanation 
'HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'HttpWebRequest' could be found (are you missing a using directive or an assembly reference?) 
GetResponse() is not supported in PCL versions. You need to use the Async version, GetResponseAsync() to get response from HttpWebRequest.

We have tried the “GetResponseAsync” to get reponse using URI string provided in your code snippet. And we have faced error while getting response due to request of URI does not have any response. So kindly use a proper URI string to create request and to get response successively.

Note: This error not relevant to DocIO portable libraries.
 
Argument 1: cannot convert from 'string' to 'System.IO.Stream' 
Essential DocIO does not provide any public API to “Save” the Word document to specified file in Xamarin. Alternatively you can use “Save” method with “Stream” overload to save the Word document using DocIO and then can save as file.

Please refer following UG links to know more about how to load and save Word document using DocIO in Xamarin.

Open Word document
Save Word document
 

Please let us know if you have any other questions.

Regards,
Sethumanikkam.Y


Sree Sundaram
Replied On February 21, 2017 03:15 AM UTC

I am still unable to run my code.  Thanks for your assistance.

Here is my code:
        public async Task<string> OpenWordFile()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.swiftview.com/tech/letterlegal5.doc");
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://valufy.file.core.windows.net/valufyfiles/ValufyReportWordTemplate.docx");
            //HttpWebResponse response = await DependencyService.Get<IPlatformService>().GetResponse(request);
            HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync();
            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();  <------- AT THIS POINT, CONTROL RETURNS WITHOUT EXECUTING THE REST OF THE CODE
            //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
            MemoryStream memoryStream = new MemoryStream();
//do something with the doc
            //Save the document into memory stream
            document.Save(memoryStream, FormatType.Docx);
            //Close the documents
            document.Close();
            Xamarin.Forms.DependencyService.Get<IPlatformService>().Save("Result.docx", "application/msword", memoryStream);

            return "success";

        }

And here's the UWP version of the Save function:

        public async Task Save(string filename, string contentType, MemoryStream stream)
        {
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile outFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            using (Stream outStream = await outFile.OpenStreamForWriteAsync())
            {
                outStream.Write(stream.ToArray(), 0, (int)stream.Length);
            }
            await Windows.System.Launcher.LaunchFileAsync(outFile);
        }


Sethumanikkam Yogendran [Syncfusion]
Replied On February 21, 2017 08:43 AM UTC

Hi Sree,

Thank you for your update.

On further analyzing with given code, retrieved response stream doesn't support writing. It causes of System.NotSupportedException thrown while closing the stream. We request you to replace “stream.Close();” with stream.Dispose(); to run the application without error.

Also we have attached resultant (Result.docx) Word document for your reference.

Please let us know if you have any other questions.

Regards,
Sethumanikkam.Y
 


Sree Sundaram
Replied On February 21, 2017 03:57 PM UTC

Thank you very much for your quick response.  That solved the problem.

Sree

Sethumanikkam Yogendran [Syncfusion]
Replied On February 22, 2017 04:06 AM UTC

Hi Sree,

Thank you for your update.

We are happy to hear that the provided solution resolved your issue. Please let us know if you need any further assistance on this. We will be happy to assist you as always.

Please let us know if you have any other questions.

Regards,
Sethumanikkam.Y


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;