Ejemplo n.º 1
0
        public ActionResult Sample05()
        {
            // 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["srcPath"];
                String url = Request.Form["url"];
                var file = Request.Files["file"];
                String destPath = Request.Form["destPath"];
                String action = Request.Form["copy"];
                // Set entered data to the results list
                result.Add("clientId", clientId);
                result.Add("privateKey", privateKey);
                result.Add("fileId", fileId);
                result.Add("destPath", destPath);
                String message = null;
                // Check is all needed fields are entered
                if (clientId == "" || privateKey == "" || destPath == "")
                {
                    // If not all fields entered send error message
                    message = "Please enter all parameters";
                    result.Add("error", message);
                    return View("Sample05", null, result);
                }
                else
                {
                    if (basePath == "")
                    {
                        basePath = "https://api.groupdocs.com/v2.0";
                    }
                    // Create service for Groupdocs account
                    GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey);
                    // Create empty variable for file name and id
                    String name = null;
                    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;
                            name = upload.AdjustedName;

                        }
                        // If upload was failed return error
                        else
                        {
                            message = "UploadFile returns error";
                            result.Add("error", message);
                            return View("Sample05", 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;
                                        name = storageInfo.Files[i].Name;
                                    }

                                }
                            }
                            else
                            {
                                message = "Get files list is failed";
                                result.Add("error", message);
                                return View("Sample05", null, result);
                            }
                        }
                        //If file wasn't uploaded return error
                        else
                        {
                            result.Add("error", "Something wrong with entered data");
                            return View("Sample05", 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, true);
                        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;
                                    name = storageInfo.Files[i].Name;
                                }

                            }
                        }
                        else
                        {
                            message = "Get files list is failed";
                            result.Add("error", message);
                            return View("Sample05", null, result);
                        }
                    }
                    // Check is entered file GuId exists
                    if (name == null || id == 0)
                    {
                        result.Add("error", "File was not found");
                        return View("Sample05", null, result);
                    }
                    // Create path to where copy/move file
                    String path = destPath + "/" + name;
                    Groupdocs.Api.Contract.FileMoveResponse transfer = new Groupdocs.Api.Contract.FileMoveResponse();
                    // Copy file if user choose copy
                    if (action == "Copy")
                    {
                        // Create empty Overide mode
                        Groupdocs.Common.OverrideMode overide = new Groupdocs.Common.OverrideMode();
                        // Make request to the Api to copy file
                        transfer = service.CopyFile(id, path, overide);
                        // Put message to result's list
                        result.Add("button", "Copied");

                    }
                    // If user choose Move file
                    else
                    {
                        Groupdocs.Common.OverrideMode overide = new Groupdocs.Common.OverrideMode();
                        transfer = service.MoveFile(id, path, overide);
                        result.Add("button", "Moved");
                    }
                    // Check is copy/move is successful
                    if (transfer.Status.Equals("Ok"))
                    {
                        // If successful put path to the copy/moved file to the results list
                        result.Add("path", path);
                    }
                    else
                    {
                        // If failed put error message
                        result.Add("error", transfer.ErrorMessage);
                    }

                }
                // Return results to the template
                return View("Sample05", null, result);

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