//### callback check for Sample37 public ActionResult sample37_callback() { // Get user info String infoFile = AppDomain.CurrentDomain.BaseDirectory + "user_info.txt"; System.IO.StreamReader userInfoFile = new System.IO.StreamReader(infoFile); String clientId = userInfoFile.ReadLine(); String privateKey = userInfoFile.ReadLine(); userInfoFile.Close(); // Check is data posted if (Request.HttpMethod == "POST") { System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream); String jsonString = reader.ReadToEnd(); var data = System.Web.Helpers.Json.Decode(jsonString); String envelopGuid = data.SourceId; envelopGuid = envelopGuid.Replace(" ", ""); String jobStatus = data.Eventtype; jobStatus = jobStatus.Replace(" ", ""); // Create service for Groupdocs account GroupdocsService service = new GroupdocsService("https://api.groupdocs.com/v2.0", clientId, privateKey); //Make request to api for get document info by job id if (jobStatus.Equals("JobCompleted")) { String downloadFolder = AppDomain.CurrentDomain.BaseDirectory + "downloads/"; //check if Downloads folder exists and remove it to clean all old files if (Directory.Exists(downloadFolder)) { Directory.Delete(downloadFolder, true); Directory.CreateDirectory(downloadFolder); } else { Directory.CreateDirectory(downloadFolder); } Groupdocs.Api.Contract.Signature.SignatureEnvelopeDocumentsResponse envelopDocuments = service.GetEnvelopeDocuments(envelopGuid); if (envelopDocuments.Status.Equals("Ok")) { String documentName = envelopDocuments.Result.Documents[0].Name; Stream getSignedDocument = service.GetSignedDocuments(envelopGuid); if (getSignedDocument.Length > 0) { FileStream writeStream = new FileStream(downloadFolder + documentName, FileMode.Create, FileAccess.Write); int Length = 256; Byte[] buffer = new Byte[Length]; int bytesRead = getSignedDocument.Read(buffer, 0, Length); // write the required bytes while (bytesRead > 0) { writeStream.Write(buffer, 0, bytesRead); bytesRead = getSignedDocument.Read(buffer, 0, Length); } getSignedDocument.Close(); writeStream.Close(); return View("sample37_callback"); } } } return View("sample37_callback"); } // If data not posted return to template for filling of necessary fields else { String jobId = clientId + ":" + privateKey; return View("sample37_callback", null, jobId); } }
public ActionResult Sample36() { // Check is data posted if (Request.HttpMethod == "POST") { //### Set variables and get POST data System.Collections.Hashtable result = new System.Collections.Hashtable(); String clientId = Request.Form["clientId"]; String privateKey = Request.Form["privateKey"]; String envelopGuid = Request.Form["envelopGuid"]; String basePath = Request.Form["basePath"]; // Set entered data to the results list result.Add("clientId", clientId); result.Add("privateKey", privateKey); result.Add("envelopGuid", envelopGuid); String message = null; // Check is all needed fields are entered if (clientId == null || privateKey == null || envelopGuid == null) { // If not all fields entered send error message message = "Please enter all parameters"; result.Add("error", message); return View("Sample36", null, result); } else { if (basePath == "") { basePath = "https://api.groupdocs.com/v2.0"; } //Make propper URL for basePath if (!basePath.Substring(basePath.Length - 3, 3).Equals("2.0")) { if (!basePath.Substring(basePath.Length - 1, 1).Equals("/")) { basePath = basePath + "/v2.0"; } else { basePath = basePath + "v2.0"; } } result.Add("basePath", basePath); // Create service for Groupdocs account GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey); String downloadFolder = AppDomain.CurrentDomain.BaseDirectory + "downloads/"; //check if Downloads folder exists and remove it to clean all old files if (Directory.Exists(downloadFolder)) { //Delete Downlaods folder Directory.Delete(downloadFolder, true); //Create empty Downloads folder Directory.CreateDirectory(downloadFolder); } //If Downloads folder not exist create it else { Directory.CreateDirectory(downloadFolder); } //Get documents info from envelop Groupdocs.Api.Contract.Signature.SignatureEnvelopeDocumentsResponse envelopDocuments = service.GetEnvelopeDocuments(envelopGuid); if (envelopDocuments.Status.Equals("Ok")) { //Get document name String documentName = envelopDocuments.Result.Documents[0].Name; //Get signed document from envelop (this method return stream) Stream getSignedDocument = service.GetSignedDocuments(envelopGuid); //Create file from stream if (getSignedDocument.Length > 0) { //Create new file FileStream writeStream = new FileStream(downloadFolder + documentName, FileMode.Create, FileAccess.Write); int Length = 256; //Create byte array with "Length" elements Byte[] buffer = new Byte[Length]; //Read bytes from stream int bytesRead = getSignedDocument.Read(buffer, 0, Length); // write the required bytes while (bytesRead > 0) { //Write stream bytes to file writeStream.Write(buffer, 0, bytesRead); //Read next bytes bytesRead = getSignedDocument.Read(buffer, 0, Length); } //Close stream getSignedDocument.Close(); //Close write stream writeStream.Close(); result.Add("documentName", documentName); message = "Files from the envelope were downloaded to server\'s local folder. You can check it <a href=\"/downloads/" + documentName + "\">here</a>"; result.Add("message", message); } else { result.Add("error", "Get signed document from envelop is failed"); return View("Sample36", null, result); } } else { result.Add("error", envelopDocuments.ErrorMessage); return View("Sample36", null, result); } return View("Sample36", null, result); } } // If data not posted return to template for filling of necessary fields else { return View("Sample36"); } }