public async Task ReadFile()
        {
            Output = string.Empty;
            this.StateHasChanged();
            var files = await fileReaderService.CreateReference(inputElement).EnumerateFilesAsync();

            var isCustomGetLengthMethodInitialized = await CurrentJSRuntime.InvokeAsync <bool>("window.hasOwnProperty", "customGetLengthMethod");

            if (!isCustomGetLengthMethodInitialized)
            {
                await WriteLine("Defining custom method 'customGetLengthMethod'");

                await CurrentJSRuntime.InvokeVoidAsync("eval",
                                                       @"window.customGetLengthMethod = (jsFileFromDotNet) => {
                            return jsFileFromDotNet.size;
                    };");
            }

            if (!files.Any())
            {
                await WriteLine("No file selected");
            }

            foreach (var file in files)
            {
                var jsFile = await file.GetJSObjectReferenceAsync();

                await using (jsFile)
                {
                    // get length manually using a custom method
                    var length = await this.CurrentJSRuntime.InvokeAsync <long>("customGetLengthMethod", jsFile);
                    await WriteLine($"Length from customGetLengthMethod: {length}");
                }
            }
        }
Exemplo n.º 2
0
        private async Task <int> ReadFileMarshalledAsync(
            int fileRef, byte[] buffer, long position, int count,
            CancellationToken cancellationToken)
        {
            var taskCompletionSource = new TaskCompletionSource <ReadFileMarshalledAsyncCallbackParams>();

            cancellationToken.Register(() => taskCompletionSource.TrySetCanceled());
            var callBackId = Guid.NewGuid().ToString("N");

            readFileMarshalledAsyncCalls[callBackId] = taskCompletionSource;
            var startCallBack = await CurrentJSRuntime.InvokeAsync <long>(
                $"FileReaderComponent.ReadFileMarshalledAsync",
                new { position, count, callBackId, fileRef });

            ReadFileMarshalledAsyncCallbackParams longResult;

            try
            {
                longResult = await taskCompletionSource.Task;
            }
            catch (Exception e)
            {
                var x = e;
                throw;
            }

            var bytesRead = 0;

            if (!string.IsNullOrEmpty(longResult.Data?.Trim()))
            {
                var byteResult = Convert.FromBase64String(longResult.Data);
                bytesRead = byteResult.Length;
                Array.Copy(byteResult, buffer, bytesRead);
            }

            return(bytesRead);
        }
Exemplo n.º 3
0
 private Task <bool> DisposeStream(int fileRef)
 {
     return(CurrentJSRuntime.InvokeAsync <bool>($"FileReaderComponent.Dispose", fileRef));
 }
Exemplo n.º 4
0
 private async Task <int> OpenReadAsync(ElementRef elementReference, int fileIndex)
 {
     return((int)await CurrentJSRuntime.InvokeAsync <long>($"FileReaderComponent.OpenRead", elementReference, fileIndex));
 }
Exemplo n.º 5
0
 public async Task <FileInfo> GetFileInfoFromReference(int fileRef)
 {
     return(Json.Deserialize <FileInfo>(await CurrentJSRuntime.InvokeAsync <string>($"FileReaderComponent.GetFileInfoFromReference", fileRef)));
 }
Exemplo n.º 6
0
 public async Task <FileInfo> GetFileInfoFromElement(ElementRef elementReference, int index)
 {
     return(Json.Deserialize <FileInfo>(await CurrentJSRuntime.InvokeAsync <string>($"FileReaderComponent.GetFileInfoFromElement", elementReference, index)));
 }
Exemplo n.º 7
0
 public async Task <int> GetFileCount(ElementRef elementReference)
 {
     return((int)await CurrentJSRuntime.InvokeAsync <long>($"FileReaderComponent.GetFileCount", elementReference));
 }
 public async Task <int> ClearValue(ElementRef elementReference)
 {
     return((int)await CurrentJSRuntime.InvokeAsync <long>($"FileReaderComponent.ClearValue", elementReference));
 }