public async void FileUpload_Tapped(object sender, EventArgs e)
        {
            try
            {
                if (Device.RuntimePlatform == Device.Android &&
                    !await this.CheckPermissionsAsync())
                {
                    return;
                }


                var crossFilePicker = CrossFilePicker.Current;
                var myResult        = await crossFilePicker.PickFile();

                if (myResult != null && !string.IsNullOrEmpty(myResult.FileName))
                {
                    string FileContent = Convert.ToBase64String(myResult.DataArray);
                    //create new file item
                    ProjectInsight.Models.Files.FileItem myFile = new ProjectInsight.Models.Files.FileItem();
                    //myFile.ItemContainer_Id = id;
                    myFile.Name = myResult.FileName;

                    //create FileUpload object for file item
                    ProjectInsight.Models.Files.FileUpload fileUpload = new ProjectInsight.Models.Files.FileUpload();
                    fileUpload.FileName = myResult.FileName;
                    fileUpload.FileContentsBase64Encoded = FileContent; //Content of the file to upload
                    myFile.UploadedFile = fileUpload;

                    //Save and return a response
                    ProjectInsight.Models.Base.Responses.ApiSaveResponse saveFileResp = FileItemService.client.Save(myFile);


                    if (saveFileResp != null && !saveFileResp.HasErrors)
                    {
                        await Navigation.PushAsync(new NewApprovalPage_Details(container_Id : saveFileResp.SavedId));
                    }
                    else if (saveFileResp != null && saveFileResp.HasErrors)
                    {
                        Common.Instance.ShowToastMessage(saveFileResp.Errors[0].ErrorMessage, DoubleResources.DangerSnackBar);
                    }
                }
                else
                {
                    //Common.Instance.ShowToastMessage("Error with file!", DoubleResources.DangerSnackBar);
                }
            }
            catch (Exception ex)
            {
                Common.Instance.ShowToastMessage(ex.ToString(), DoubleResources.DangerSnackBar);
            }
        }
예제 #2
0
        public static async Task <ApiSaveResponse> UploadFile(string dataArray, string filePath, string fileName, Guid id)
        {
            //create new file item
            ProjectInsight.Models.Files.FileItem myFile = new ProjectInsight.Models.Files.FileItem();
            myFile.ItemContainer_Id = id;
            myFile.Name             = fileName;

            //create FileUpload object for file item
            ProjectInsight.Models.Files.FileUpload fileUpload = new ProjectInsight.Models.Files.FileUpload();
            fileUpload.FileName = fileName;
            fileUpload.FileContentsBase64Encoded = dataArray; //Content of the file to upload
            myFile.UploadedFile = fileUpload;

            //Save and return a response
            ProjectInsight.Models.Base.Responses.ApiSaveResponse saveFileResp = new ProjectInsight.Models.Base.Responses.ApiSaveResponse();
            saveFileResp = client.ProjectInsightWebApiClient.FileItem.Save(myFile);

            return(saveFileResp);
        }