Пример #1
0
        public ActionResult Sample08()
        {
            // 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 fileGuId = "";
                String pageNumber = Request.Form["pageNumber"];
                result.Add("clientId", clientId);
                result.Add("privateKey", privateKey);
                result.Add("pageNumber", pageNumber);
                String message = null;
                // Check is all needed fields are entered
                if (clientId == null || privateKey == null || fileId == null)
                {
                    // If not all fields entered send error message
                    message = "Please enter all parameters";
                    result.Add("error", message);
                    return View("Sample08", null, result);
                }
                else
                {
                    if (basePath == "")
                    {
                        basePath = "https://api.groupdocs.com/v2.0";
                    }
                    // Create service for Groupdocs account
                    GroupdocsService service = new GroupdocsService(basePath, clientId, privateKey);
                    //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
                            fileGuId = upload.Guid;

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

                                    }

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

                                }

                            }
                        }
                        else
                        {
                            message = "Get files list is failed";
                            result.Add("error", message);
                            return View("Sample08", null, result);
                        }
                    }
                    // Make request to get page image url
                    String[] pageUrl = service.GetDocumentPagesImageUrls(fileGuId, "600x750", String.Empty, Int32.Parse(pageNumber), 1, 100, false);
                    // Check if url is not null
                    if (pageUrl != null)
                    {
                        // Redirect to template with receive URL
                        result.Add("url", pageUrl[0]);
                        return View("Sample08", null, result);
                    }
                    // Else return error to the template
                    else
                    {
                        message = "Something is wrong with your data";
                        result.Add("error", message);
                        return View("Sample08", null, result);
                    }

                }

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