GithubHelp home page GithubHelp logo

ajipsum / blazorfilereader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tewr/blazorfilereader

0.0 1.0 0.0 13.11 MB

Read-only File streams in Blazor - Library and Demo

License: MIT License

C# 47.58% TypeScript 6.63% HTML 35.34% CSS 10.45%

blazorfilereader's Introduction

Build status NuGet Donate

BlazorFileReader

Blazor library exposing read-only file streams in Blazor.

This library exposes read-only streams using <input type="file" /> and FileReader.

Originally built for Wasm ("Client-side" Blazor), Server-side Blazor (previously aka RazorComponents) is also supported as of version 0.7.1.

Here is a Live demo that contains the output of the wasm demo project. Currently, its a build based on v0.5.1.

Installation

0.12.0 is a pre-release version. First of all, make sure your environment is up to date with the appropriate SDK and VS2019 preview 4. See this article for more details. Depending on your project type, use one of the two examples below. For a complete use-case, see the client or server-side demo projects.

Client-side / Wasm Project type

Use Nuget: Install-Package Tewr.Blazor.FileReader

Setup IoC for IFileReaderServiceas in (Startup.cs):

services.AddFileReaderService();

Server-side / asp.net core Project type

Use Nuget: Install-Package Tewr.Blazor.FileReader

Setup IoC for IFileReaderService as in the example (Startup.cs):

services.AddFileReaderService(options => options.InitializeOnFirstCall = true);

Usage in a Blazor View

The code for views looks the same for both client- and server-side projects.

@page "/MyPage"
@using System.IO;
@inject IFileReaderService fileReaderService;

<input type="file" @ref="@inputTypeFileElement" /><button @onclick="@ReadFile">Read file</button>

@functions {
    ElementRef inputTypeFileElement;

    public async Task ReadFile()
    {
        foreach (var file in await fileReaderService.CreateReference(inputTypeFileElement).EnumerateFilesAsync())
        {
            // Read into buffer and act (uses less memory)
            using(Stream stream = await file.OpenReadAsync()) {
                // Do (async) stuff with stream...
                await stream.ReadAsync(buffer, ...);
                // The following will fail. Only async read is allowed.
                stream.Read(buffer, ...)
            }

            // Read into memory and act
            using(MemoryStream memoryStream = await file.CreateMemoryStreamAsync(4096)) {
                // Sync calls are ok once file is in memory
                memoryStream.Read(buffer, ...)
            }
        }
    }
}

Notes

To use the code in this demo in your own project you need to use at least version 0.4.0 of blazor (see branch 0.4.0).

The master branch uses v3.0.0-preview6.19307.2 of Blazor.

Blazor is an experimental preview project, not ready for production use. Just as Blazor API frequently has breaking changes, so does the API of this library.

Version notes

Version 0.12.19186 fixes an issue with server-side setup which was only visible when having multiple users.

Version 0.12.19168 adds support for sdk 3.0.0-preview6.19307.2, and several issues are resolved with this release, notably meticulous setup and issues with buffer size for server-side projects. Also, the Wasm helper package has been deprecated.

Version 0.11.0 adds support for sdk 3.0.0-preview5-19227-01. It also introduces a tiny feature: The IFileReaderRef.ClearValue() method, used to clear the value of a referenced file input. Also, fixes a bug in Edge and a package issue.

Version 0.10.0 adds support for sdk v3.0.0-preview-4-19216-03

Versions 0.9.0 introduces a small helper-package for the IoC setup of Wasm, injecting an implementation of IInvokeUnmarshalled.

Versions 0.8.0 requires copy-paste implementation of IInvokeUnmarshalled.

Versions previous to 0.7.1 did not support server-side Blazor and would throw [System.PlatformNotSupportedException] Requires MonoWebAssemblyJSRuntime as the JSRuntime.

Versions previous to 0.5.1 wrapped the input element in a Blazor Component, this has been removed for better configurability and general lack of value.

blazorfilereader's People

Contributors

tewr avatar pfeurean avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.