Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. [HttpPost]
  2. public IHttpActionResult GetData(DocusignViewModel docusignViewModel)
  3. {
  4. Docusign docusign = new Docusign();
  5. HtmlHelper.WriteLog("============= Begin [GetData] ================");
  6. HtmlHelper.WriteLog("[GetData] DataPost: " + JsonConvert.SerializeObject(docusignViewModel));
  7. try
  8. {
  9. string username = ConfigurationManager.AppSettings["usernameDocusign"];
  10. string password = ConfigurationManager.AppSettings["passwordDocusign"];
  11. string integratorKey = ConfigurationManager.AppSettings["integratorKey"];
  12. string basePath = ConfigurationManager.AppSettings["basePath"];
  13. string urlWebhook = ConfigurationManager.AppSettings["urlWebhook"];
  14. string downPath = ConfigurationManager.AppSettings["downloadPath"];
  15. string urlWebsite = ConfigurationManager.AppSettings["urlWebsite"].TrimEnd('/');
  16.  
  17. downPath = Path.Combine(downPath, "docusign");
  18. if (!Directory.Exists(downPath)) Directory.CreateDirectory(downPath);
  19. string accountId = docusign.LoginApi(username, password, integratorKey, basePath);
  20. HtmlHelper.WriteLog("[GetData] accountId: " + accountId);
  21. var roles = docusignViewModel.Roles;
  22. var envelopeId = string.Empty;
  23. var message = string.Empty;
  24. var fileTemp = string.Empty;
  25.  
  26. if (roles != null)
  27. {
  28. envelopeId = docusign.CreateDocumentByTemplate(accountId, docusignViewModel.TemplateId, docusignViewModel.DocumentName, roles , urlWebhook);
  29. HtmlHelper.WriteLog("[GetData] envelopeId: " + envelopeId);
  30. }
  31. var checkDownload = docusign.DownLoadFilesExcludeCertificate(accountId, envelopeId, downPath,
  32. docusignViewModel.DocumentName, out message, out fileTemp);
  33. HtmlHelper.WriteLog("[GetData] checkDownload: " + checkDownload + " Message download:" + message);
  34. HtmlHelper.WriteLog("[GetData] fileTemp: " + fileTemp);
  35. urlWebsite = urlWebsite + "/docusign/" + docusignViewModel.DocumentName;
  36. HtmlHelper.WriteLog("[GetData] urlWebsite: " + urlWebsite);
  37. if (checkDownload)
  38. {
  39. HtmlHelper.WriteLog("============= End [GetData] ================");
  40. return Ok(urlWebsite);
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. HtmlHelper.WriteLog("[GetData] Error: " + ex.Message);
  46. }
  47. HtmlHelper.WriteLog("============= End [GetData] ================");
  48. return NotFound();
  49. }
  50.  
  51. EnvelopesApi envelopesApi = new EnvelopesApi();
  52. //Check documents
  53. EnvelopeDocumentsResult docsList = envelopesApi.ListDocuments(accountId, envelopeId);
  54. int docCount = docsList.EnvelopeDocuments.Count;
  55. //Check and Create filePath
  56. string filePath = path;
  57. if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
  58. FileStream fs = null;
  59. string[] filesName = new string[docCount-1];
  60. fileOutput = Path.Combine(path, fileName);
  61. // loop through the envelope's documents and download each doc
  62. for (int i = 0; i < docCount-1; i++)
  63. {
  64. // GetDocument() API call returns a MemoryStream
  65. MemoryStream docStream = (MemoryStream)envelopesApi.GetDocument(accountId, envelopeId, docsList.EnvelopeDocuments[i].DocumentId);
  66. filePath = Path.Combine(path, "Docusign_" + DateTime.Now.Ticks + ".pdf");
  67. // let's save the document to local file system
  68. //filePath = Path.GetTempPath() + Path.GetRandomFileName() + ".pdf";
  69. fs = new FileStream(filePath, FileMode.Create);
  70. docStream.Seek(0, SeekOrigin.Begin);
  71. docStream.CopyTo(fs);
  72. filesName[i] = filePath;
  73. fs.Close();
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement