This is my service to export word to browser client .when client call webservice.
[WebMethod]
public void XemDonDangKy(string MaDonVi, string HoSoID, string DichVuCongID, string KhachHangID, HttpResponse Response)
{
string DonViID = ConfigurationManager.AppSettings[MaDonVi + "ID"].ToString();
//Instantiate an instance of license and set the license file through its path
License license = new License();
string licenseAsposePath = ConfigurationManager.AppSettings["licenseAsposePath"];
license.SetLicense(licenseAsposePath);
//Open template
string sharePointListPath = ConfigurationManager.AppSettings["uploadedFilePath"];
string filename = GetTemplateFileName(MaDonVi, DonViID, DichVuCongID).Tables[0].Rows[0]["TemplateName"].ToString();
string url = sharePointListPath + filename;
//Create document
Document doc = new Document(url);
DataSet ds = new DataSet();
//thong tin khach hang
DataTable dt = GetThongTinhKHachHangMerg(ConfigurationManager.AppSettings["HCM"], KhachHangID).Tables[0];
dt.TableName = "KH";
ds.Tables.Add(dt.Copy());
//thong tin ho so
DataTable dt2 = GetThongTinHoSoMerg(MaDonVi, HoSoID, DichVuCongID).Tables[0];
dt2.TableName = "HS";
ds.Tables.Add(dt2.Copy());
// Fill the fields in the document with user data.
doc.MailMerge.ExecuteWithRegions(ds);
// Send the document in Word format to the client browser with an option to save to disk or open inside the current browser.
doc.Save("InBieNhan_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".doc", Aspose.Words.SaveFormat.Doc, Aspose.Words.SaveType.OpenInWord, Response);
}
and i get error like that
System.Web.HttpResponse cannot be serialized because it does not have a parameterless constructor
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:
[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;
}
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();
}