Web API Edition | ComponentOne
Services / Cloud Services / Perform Operations
In This Topic
    Perform Operations
    In This Topic

    Cloud API allows you to perform the following operations:

    List Files

    1. Create an API method, say GetCloudData, to fetch or read the list of files and folders from the container.
      C#
      Copy Code
      public class FolderFiles 
      { 
              public string ParentPath { get; set; } 
              public string Name { get; set; } 
              public int Type { get; set; } 
              public FolderFiles[] SubFolder { get; set; } 
              
              //ItemID is required only in case of Google Drive
              public string ItemID { get; set; } 
              
              public FolderFiles() 
              { 
                  ParentPath = null; 
              } 
      } 
       
      public async Task<List<FolderFiles>> GetCloudData(string cloudName) 
      { 
                  var applicationInitPath  ="FolderName";  // Initial path from where file list should be fetched
                  string authority = System.Web.HttpContext.Current.Request.Url.Authority; 
                  string apiUrl = authority + "/api/storage/List"; 
                  string parentPath = "/" + cloudName + "/" + applicationInitPath); 
      
                  using (HttpClient client = new HttpClient()) 
                  { 
                      var url = new UriBuilder("http://" + apiUrl + parentPath); 
                      var response = await client.GetAsync(url.ToString()).ConfigureAwait(false); 
      
                      if (response.IsSuccessStatusCode) 
                      { 
                          var data = await response.Content.ReadAsStringAsync().ConfigureAwait(false); 
                          var table = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FolderFiles>>(data); 
      
                          foreach (var row in table) 
                          { 
                              row.ParentPath = parentPath; 
                              if (row.Type == 0) 
                              { 
                                  row.SubFolder = new FolderFiles[0]; 
                              } 
                          } 
                          return table; 
                      } 
                  } 
                  return new List<FolderFiles>(); 
      }
      
    2. Create a method, say LazyLoading_GetCloudData, for fetching details for the specified folder.
      C#
      Copy Code
      public async Task< List<FolderFiles>> LazyLoading_GetCloudData(string subPath, string parentPath, string cloudName, string itemId) 
              { 
                  string authority = System.Web.HttpContext.Current.Request.Url.Authority; 
                  string apiUrl = "http://" + authority + "/api/storage/List"; 
      
                  using (HttpClient client = new HttpClient()) 
                  { 
                      var url = new UriBuilder(apiUrl + parentPath); 
                                      
                      if (!string.IsNullOrEmpty(subPath)) 
                          url = new UriBuilder(url + "/?subpath=" + subPath); 
      
                      //While working with Google drive, itemId is required to make sure in which folder is working
                      if (!string.IsNullOrEmpty(itemId)) 
                          url = new UriBuilder(url + "?itemid=" + itemId); 
      
       
                      var response = await client.GetAsync(url.ToString()).ConfigureAwait(false); 
      
                      if (response.IsSuccessStatusCode) 
                      { 
                          var data = await response.Content.ReadAsStringAsync().ConfigureAwait(false); 
                          var table = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FolderFiles>>(data); 
      
                          foreach (var row in table) 
                          { 
                              row.ParentPath = parentPath + "/" + subPath; 
                              if (row.Type == 0) 
                              { 
                                  row.SubFolder = new FolderFiles[0];                         
                              } 
                          } 
                          return table; 
                      } 
                  } 
                  return new List<FolderFiles>(); 
      }
      

    Upload File

    To upload a file to the container, call C1WebAPI and refer to the specified location in code:

    C#
    Copy Code
    var baseUrl = window.location.origin;//"http://localhost:9272/";
    
    function Upload() { 
                var file = document.getElementById("file").files[0]; 
                var data = new FormData(); 
                data.append("file", file); 
    
                var url = baseUrl+'api/storage/' + cloudName + "/" + folderPathToUploadFile+ "/" + file.name; 
                            
                //This code is only for using Google Drive service
                if (itemId != null) { 
                    url = url + "?itemid=" + itemId; 
                } 
    
                $.ajax({ 
                    url: url, 
                    type: 'POST', 
                    data: data, 
                    cache: false, 
                    contentType: false, 
                    processData: false 
                                    
                }).then(function (res) { 
                    alert("Uploaded") 
                }) 
            }
    

    Download File

    To download a file, create anchor element with the URL “api/storage”+ filepath and call click method using the following code:

    C#
    Copy Code
    var baseUrl = window.location.origin;//"http://localhost:9272/"; 
    function Download() { 
                    var url = baseUrl+'/api/storage/' + cloudName + "/" + folderPathToDownloadFile + "/" + FileNameToDownloadwithExtension;  
                    var elem = wijmo.createElement(""); 
                    elem.click(); 
                }
    

    Delete File

    To delete a file, use the following code:

    C#
    Copy Code
    var baseUrl = window.location.origin;//"http://localhost:9272/"; 
    function DeleteFile() { 
                    var url = baseUrl+'api/storage/' + cloudName + "/" + folderPathToDeleteFile + "/?subpath=" + FileNameToDeleteWithExtn; 
            
                    //This code is only for using Google Drive service
                    if (itemId != null) { 
                         url = url + "?itemid=" + itemId; 
                    } 
    
                    $.ajax({ 
                        url: url, 
                        type: 'DELETE', 
                        cache: false, 
                        contentType: false, 
                        processData: false, 
    
                        success: function (data, success, obj) { 
                            alert("Deleted"); 
                            refreshTree(); 
                        } 
                    }); 
                }