Hello,
I’ve try to save a document through a web method without success
Public Shared Function GetFileToPublishFromJs(ByVal fileName As String)
Dim pathName As String = String.Format("{0}\{1}", Repertoire, fileName)
Dim doc As Document = New Document(pathName)
doc.Save("out.doc", SaveFormat.FormatDocument, SaveType.OpenInWord, HttpContext.Current.Response)
Return fileName
End Sub
Is that possible ?
Regards
Hi
Thanks for your request. It is impossible to use Response in the WebMethod. If you would like to return file form WebMethod then you should return file bytes. Here are steps you should follow.
- Create web service and create method (for example GetDocument). This method will create or open the document and return its byte array. Please see the following code:
[C#]
[WebMethod]
public byte[] GetDocument()
{
// Create or open document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello world");
// Save document into MemoryStream
MemoryStream docStrm = new MemoryStream();
doc.Save(docStrm, SaveFormat.Doc);
// Get file bytes
byte[] docBytes = docStrm.GetBuffer();
return docBytes;
}
[VB]
Public Function GetDocument() As Byte()
'Create or open document
Dim doc As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
builder.Write("Hello world")
'Save document into MemoryStream
Dim docStrm As MemoryStream = New MemoryStream()
doc.Save(docStrm, SaveFormat.Doc)
'Get file bytes
Dim docBytes As Byte() = docStrm.GetBuffer()
Return docBytes
End Function
- Create virtual directory for your Web Service. On my side link to the newly created web service is:
http://localhost/GetDocumentService/Service.asmx
- Create console application (just for testing, it may be any type of application). Add Web Reference to your Web Service. You should insert link to your service and give the name. (I use GetDocumentService name)
- Now you can call your WebMethod from your console application. See the following code:
[C#]
static void Main(string[] args)
{
// Create instance of web service
GetDocumentService.Service docService = new GetDocumentService.Service();
// Get document bytes
byte[] docBytes = docService.GetDocument();
// Save file
FileStream docFile = new FileStream(@"C:\Temp\out.doc", FileMode.Create);
docFile.Write(docBytes, 0, docBytes.Length);
docFile.Close();
}
[VB]
Sub Main()
'Create instance of web service
Dim docService As GetDocumentService.Service = New GetDocumentService.Service()
'Get document bytes
Dim docBytes As Byte() = docService.GetDocument()
'Save file
Dim docFile As FileStream = New FileStream("C:\Temp\out.doc", FileMode.Create)
docFile.Write(docBytes, 0, docBytes.Length)
docFile.Close()
End Sub
Hope this could help you.
Best regards.
Also maybe the following article could be useful for you.
http://www.zdnetasia.com/techguide/webdev/0,39044903,39251815,00.htm
Best regards.
Thanks Alexey,
I will try this (I am always amazed by your quick responses
Regards