Hi
Mohanaselvam,
that works great.
But when implemented in my project i have the following problem:
In your example, you call the button submit on Index action.
I call it on a different action, and that seems to cause the Sample.docx not to be generated.
There are no errors, my controller action gets called and populates the base64 string with the correct value from the database (using dapper)
But nothing happens on return.
There is no file generated.
The same code, when placed on Index action works correctly (without the parameter in Index() and with hard-coded id.
This is the JS:
$("#GridMaster").ejGrid({
recordClick: function (args) {
if (args.cellIndex === 0) {
$.ajax({
contentType: 'application/json; charset=utf-8',
url: "FDI/ExportToWord/",
method: 'POST',
dataType: 'json',
data: JSON.stringify(args.data.FDIQuestionnaireID)
});
}
}
});
And the controller part:
public ActionResult ExportToWord([FromBody]int id)
{
string rootPath = _hostingEnvironment.WebRootPath;
FileStream fileStream = new FileStream(rootPath + "\\Template\\FDI_HR.docx", FileMode.Open);
WordDocument document = new WordDocument(fileStream, FormatType.Docx);
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark("signature");
using (var connection = new SqlConnection(Global.SQLConnection))
{
connection.Open();
string base64 = connection.QueryFirstOrDefault<string>("Signature_Select", new { FDIQuestionnaireID = id }, commandType: CommandType.StoredProcedure);
WPicture picture = bookmarkNavigator.InsertParagraphItem(ParagraphItemType.Picture) as WPicture;
picture.LoadImage(Convert.FromBase64String(base64));
MemoryStream memoryStream = new MemoryStream();
document.Save(memoryStream, FormatType.Docx);
document.Close();
fileStream.Dispose();
memoryStream.Position = 0;
return File(memoryStream, "application/msword", "Sample.docx");
}
}
Can you help please