Exemple #1
0
        async Task SaveImage(byte[] image, String fileName, PCLStorage.IFolder rootFolder = null)
        {
            // get hold of the file system
            PCLStorage.IFolder folder = rootFolder ?? PCLStorage.FileSystem.Current.LocalStorage;

            // create a file, overwriting any existing file
            PCLStorage.IFile file = await folder.CreateFileAsync(fileName, PCLStorage.CreationCollisionOption.ReplaceExisting);

            // populate the file with image data
            using (System.IO.Stream stream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
            {
                stream.Write(image, 0, image.Length);
            }
        }
Exemple #2
0
        async void OnSelectFileCommand(object sender, EventArgs e)
        {
            try
            {
                PCLStorage.IFolder folder     = PCLStorage.FileSystem.Current.LocalStorage;
                string             rootFolder = folder.Path;
                //txtSelectedFile.Text = rootFolder;

                Plugin.FilePicker.Abstractions.FileData fileData = await Plugin.FilePicker.CrossFilePicker.Current.PickFile();

                //string fileName = null;
                if (fileData != null)
                {
                    string filePath = fileData.FilePath;
                    string fileName = fileData.FileName;

                    //string filePath = "/storage/sdcard0/MyFavorite/Photo Card-1.pdf";
                    string encodedFilePath = GetTruePath(filePath);

                    //_viewModel.Uri = string.Format("file:///{0}", encodedFilePath);

                    PCLStorage.IFolder folder2 = await folder.CreateFolderAsync("root", PCLStorage.CreationCollisionOption.ReplaceExisting);
                    await SaveImage(fileData.DataArray, fileName, folder2);


                    txtSelectedFile.Text = folder2.Path + "/" + fileName;
                    // _viewModel.Uri = string.Format("file:///{0}", GetTruePath(txtSelectedFile.Text));
                }
                else
                {
                    txtSelectedFile.Text = null;
                }

                //txtSelectedFile.Text = _viewModel.Uri;
            }
            catch (Exception ex)
            {
                await DisplayAlert("ERROR", ex.ToString(), "Ok");
            }
        }
Exemple #3
0
        public static async Task <string> DownloadDocument(string docPath, string _token, AuditoriaModel _regAud)
        {
            string _messageResult = string.Empty;
            string _urlPath       = docPath.Replace("C:\\BCMWEB", "https://www.bcmweb.net").Replace("\\", "/");
            int    _startPos      = docPath.LastIndexOf("\\");
            string _docName       = docPath.Substring(_startPos + 1);

            try
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _token);
                    httpClient.Timeout = TimeSpan.FromMinutes(30);
                    var _cancelTokenSource = new CancellationTokenSource();
                    var _cancelToken       = _cancelTokenSource.Token;
                    var request            = new HttpRequestMessage(new HttpMethod("GET"), _urlPath);
                    var result             = await httpClient.SendAsync(request, _cancelToken).ConfigureAwait(true);

                    if (result.IsSuccessStatusCode)
                    {
                        string             _folderPath = DependencyService.Get <IIDownloadPathService>().GetDownloadFolder();
                        PCLStorage.IFolder _destFolder = await PCLStorage.FileSystem.Current.GetFolderFromPathAsync(_folderPath);

                        //IFolder _folder = await _destFolder.CreateFolderAsync("BCMWebDocs", CreationCollisionOption.OpenIfExists);
                        PCLStorage.IFile _file = await _destFolder.CreateFileAsync(_docName, PCLStorage.CreationCollisionOption.ReplaceExisting);

                        using (var fileHandler = await _file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite, default(CancellationToken)))
                        {
                            byte[] _fileBuffer = await result.Content.ReadAsByteArrayAsync();

                            await fileHandler.WriteAsync(_fileBuffer, 0, _fileBuffer.Length);
                        }

                        string serData = JsonConvert.SerializeObject(_regAud);
                        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        string auditoriaURL = "https://www.bcmweb.net/api/auditoria/add";

                        request = new HttpRequestMessage(new HttpMethod("POST"), auditoriaURL)
                        {
                            Content = new StringContent(serData, Encoding.UTF8, "application/json")
                        };
                        result = await httpClient.SendAsync(request, _cancelToken).ConfigureAwait(true);

                        _messageResult = string.Format("El documento fue descargado satisfactoriamente, y ubicado en \"{0}\"", _folderPath);
                    }
                }
            }
            catch (TaskCanceledException tex)
            {
                if (!tex.CancellationToken.IsCancellationRequested)
                {
                    _messageResult = "La conexión a internet no permite descargar el archivo";
                }
            }
            catch (Exception ex)
            {
                string _msg = ex.Message;
                if (ex.Message.ToLowerInvariant().Contains("instance"))
                {
                    //_messageResult = "Error descargando el documento. Notifique a Soporte";
                    _messageResult = ex.Message;
                }
                else if (ex.Message.ToLowerInvariant().Contains("task"))
                {
                    _messageResult = "No se pudo descargar el documento. Verifique su acceso a internet";
                }
                else
                {
                    ex.Message.ToLowerInvariant().Contains("instance");
                }
            }

            return(_messageResult);
        }