Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- public IHttpActionResult GetData(DocusignViewModel docusignViewModel)
- {
- Docusign docusign = new Docusign();
- HtmlHelper.WriteLog("============= Begin [GetData] ================");
- HtmlHelper.WriteLog("[GetData] DataPost: " + JsonConvert.SerializeObject(docusignViewModel));
- try
- {
- string username = ConfigurationManager.AppSettings["usernameDocusign"];
- string password = ConfigurationManager.AppSettings["passwordDocusign"];
- string integratorKey = ConfigurationManager.AppSettings["integratorKey"];
- string basePath = ConfigurationManager.AppSettings["basePath"];
- string urlWebhook = ConfigurationManager.AppSettings["urlWebhook"];
- string downPath = ConfigurationManager.AppSettings["downloadPath"];
- string urlWebsite = ConfigurationManager.AppSettings["urlWebsite"].TrimEnd('/');
- downPath = Path.Combine(downPath, "docusign");
- if (!Directory.Exists(downPath)) Directory.CreateDirectory(downPath);
- string accountId = docusign.LoginApi(username, password, integratorKey, basePath);
- HtmlHelper.WriteLog("[GetData] accountId: " + accountId);
- var roles = docusignViewModel.Roles;
- var envelopeId = string.Empty;
- var message = string.Empty;
- var fileTemp = string.Empty;
- if (roles != null)
- {
- envelopeId = docusign.CreateDocumentByTemplate(accountId, docusignViewModel.TemplateId, docusignViewModel.DocumentName, roles , urlWebhook);
- HtmlHelper.WriteLog("[GetData] envelopeId: " + envelopeId);
- }
- var checkDownload = docusign.DownLoadFilesExcludeCertificate(accountId, envelopeId, downPath,
- docusignViewModel.DocumentName, out message, out fileTemp);
- HtmlHelper.WriteLog("[GetData] checkDownload: " + checkDownload + " Message download:" + message);
- HtmlHelper.WriteLog("[GetData] fileTemp: " + fileTemp);
- urlWebsite = urlWebsite + "/docusign/" + docusignViewModel.DocumentName;
- HtmlHelper.WriteLog("[GetData] urlWebsite: " + urlWebsite);
- if (checkDownload)
- {
- HtmlHelper.WriteLog("============= End [GetData] ================");
- return Ok(urlWebsite);
- }
- }
- catch (Exception ex)
- {
- HtmlHelper.WriteLog("[GetData] Error: " + ex.Message);
- }
- HtmlHelper.WriteLog("============= End [GetData] ================");
- return NotFound();
- }
- EnvelopesApi envelopesApi = new EnvelopesApi();
- //Check documents
- EnvelopeDocumentsResult docsList = envelopesApi.ListDocuments(accountId, envelopeId);
- int docCount = docsList.EnvelopeDocuments.Count;
- //Check and Create filePath
- string filePath = path;
- if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
- FileStream fs = null;
- string[] filesName = new string[docCount-1];
- fileOutput = Path.Combine(path, fileName);
- // loop through the envelope's documents and download each doc
- for (int i = 0; i < docCount-1; i++)
- {
- // GetDocument() API call returns a MemoryStream
- MemoryStream docStream = (MemoryStream)envelopesApi.GetDocument(accountId, envelopeId, docsList.EnvelopeDocuments[i].DocumentId);
- filePath = Path.Combine(path, "Docusign_" + DateTime.Now.Ticks + ".pdf");
- // let's save the document to local file system
- //filePath = Path.GetTempPath() + Path.GetRandomFileName() + ".pdf";
- fs = new FileStream(filePath, FileMode.Create);
- docStream.Seek(0, SeekOrigin.Begin);
- docStream.CopyTo(fs);
- filesName[i] = filePath;
- fs.Close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement