private void Create(string format) { try { if (string.IsNullOrEmpty(BuildHelper.BuilderPath)) { throw new Exception("Empty Builder Path"); } var name = (NameText.Text ?? "").Trim(); if (string.IsNullOrEmpty(name)) { name = "John Smith"; } var company = (CompanyText.Text ?? "").Trim(); if (string.IsNullOrEmpty(company)) { company = "ONLYOFFICE"; } var title = (TitleText.Text ?? "").Trim(); if (string.IsNullOrEmpty(title)) { title = "Commercial director"; } var filePath = BuildHelper.CreateDocument(BuildHelper.BuilderPath, name, company, title, format); var fileName = Path.GetFileName(filePath) ?? "output..docx"; fileName = "Sample" + fileName.Substring(fileName.IndexOf('.', 7)); var mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; if (Path.GetExtension(fileName) == ".xlsx") { mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; } else if (Path.GetExtension(fileName) == ".pdf") { mime = "application/pdf"; } Response.ContentType = mime; Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); Response.TransmitFile(filePath); Response.End(); } catch (Exception exception) { ErrorMessage = exception.Message; } }
public JsonResult Create(UserInfo userInfo) { try { if (string.IsNullOrEmpty(Settings.BuilderPath)) { throw new Exception("Empty Builder Path"); } if (userInfo == null) { throw new ArgumentException("userInfo"); } if (string.IsNullOrEmpty(userInfo.Name)) { throw new Exception("Empty Name"); } if (string.IsNullOrEmpty(userInfo.Company)) { throw new Exception("Empty Company"); } if (string.IsNullOrEmpty(userInfo.Title)) { throw new Exception("Empty Title"); } var filePath = BuildHelper.CreateDocument(Settings.BuilderPath, userInfo); return(Json(new { success = true, message = filePath })); } catch (Exception ex) { LogHelper.Log.Error(ex.Message, ex); return(Json(new { success = false, message = ex.Message })); } }