예제 #1
0
        public ActionResult Sample10()
        {
            // 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 basePath = Request.Form["basePath"];
                String fileId = Request.Form["fileId"];
                String url = Request.Form["url"];
                var file = Request.Files["file"];
                String email = Request.Form["email"];
                // Set entered data to the results list
                result.Add("clientId", clientId);
                result.Add("privateKey", privateKey);
                result.Add("fileId", fileId);
                result.Add("email", email);
                String message = null;
                // Check is all needed fields are entered
                if (clientId == null || privateKey == null || email == null)
                {
                    // If not all fields entered send error message
                    message = "Please enter all parameters";
                    result.Add("error", message);
                    return View("Sample10", null, result);
                }
                else
                {
                    // Create string array with emails
                    String[] sharers = new String[1];
                    sharers[0] = email;
                    if (basePath == "")
                    {
                        basePath = "https://api.groupdocs.com/v2.0";
                    }
                    // Create service for Groupdocs account
                    GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey);
                    decimal id = new decimal();
                    //Check is chosen local file
                    if (!file.ContentLength.Equals(0))
                    {
                        // Upload file with empty description.
                        Groupdocs.Api.Contract.UploadRequestResult upload = service.UploadFile(file.FileName, String.Empty, file.InputStream);
                        // Check is upload successful
                        if (upload.Guid != null)
                        {
                            // Put uploaded file GuId to the result's list
                            id = upload.Id;

                        }
                        // If upload was failed return error
                        else
                        {
                            message = "UploadFile returns error";
                            result.Add("error", message);
                            return View("Sample10", null, result);
                        }

                    }
                    //Check is url entered
                    if (!url.Equals(""))
                    {
                        //Make request to upload file from entered Url
                        String guid = service.UploadUrl(url);
                        if (guid != null)
                        {
                            //Get all files from GroupDocs account
                            Groupdocs.Api.Contract.ListEntitiesResult storageInfo = service.GetFileSystemEntities("My Web Documents", 0, -1, null, false, null, null, true);
                            if (storageInfo.Files.Length > 0)
                            {
                                // Get file id by uploaded file GuId
                                for (int i = 0; i < storageInfo.Files.Length; i++)
                                {
                                    if (storageInfo.Files[i].Guid == guid)
                                    {
                                        id = storageInfo.Files[i].Id;

                                    }

                                }
                            }
                            else
                            {
                                message = "Get files list is failed";
                                result.Add("error", message);
                                return View("Sample10", null, result);
                            }
                        }
                        //If file wasn't uploaded return error
                        else
                        {
                            result.Add("error", "Something wrong with entered data");
                            return View("Sample10", null, result);
                        }
                    }
                    //Check is file guid entered
                    if (!fileId.Equals(""))
                    {
                        //Get all files from GroupDocs account
                        Groupdocs.Api.Contract.ListEntitiesResult storageInfo = service.GetFileSystemEntities("", 0, -1, null, false, null, null, false);
                        if (storageInfo.Files.Length > 0)
                        {
                            // Get file id and name by entered file GuId
                            for (int i = 0; i < storageInfo.Files.Length; i++)
                            {
                                if (storageInfo.Files[i].Guid == fileId)
                                {
                                    id = storageInfo.Files[i].Id;

                                }
                                else
                                {
                                    message = "Selected file was not found";
                                    result.Add("error", message);
                                    return View("Sample10", null, result);
                                }

                            }
                        }
                        else
                        {
                            message = "Get files list is failed";
                            result.Add("error", message);
                            return View("Sample10", null, result);
                        }
                    }
                    // Make request to share document
                    Groupdocs.Api.Contract.SharedUsersResult share = service.ShareDocument(id, sharers);
                    // Check is request return data
                    if (share != null)
                    {
                        // Return primary email to the template
                        result.Add("sharer", share.SharedUsers[0].PrimaryEmail);
                        return View("Sample10", null, result);
                    }
                    // If request return's null return error to the template
                    else
                    {
                        message = "Something is wrong with your data";
                        result.Add("error", message);
                        return View("Sample10", null, result);
                    }

                }

            }
            // If data not posted return to template for filling of necessary fields
            else
            {
                return View("Sample10");
            }
        }