DataJuggler / BlazorFileUpload

This was originally a wrapper of Steve Sanderson's BlazorFileInput. It has since been upgraded to a wrapper of ASP.NET CORE FileInput
32 stars 11 forks source link

How to get the stream instead of saving? #9

Open VR-Architect opened 4 years ago

VR-Architect commented 4 years ago

Thanks for the nice control!

Do you have an example where we can capture the file stream instead of writing the file to the Upload folder? I am trying to use the upload to save a CSV data to an SQL table.

Thanks again!

DataJuggler commented 4 years ago

Thanks for the kind words. Steve Sanderson created the magic, I just made it a little easier to pull the rabbit out of the hat :).

I did just build a Blazor Image Gallery that uses SQL Server to store the Artists and Images, but I still save the file to different places under wwwroot to answer your direct question:

Artists wwwroot/Images/Artists

Gallery wwwroot/Images/Gallery/[Artist.Name]/

If you look here around line 241 is where the memory stream is saved to a file:

DataJuggler.Blazor.FileUpload.cs On Git Hub

I guess an option could be created for SaveToDisk, and the UploadFileInfo could return the memory stream as a property. It sounds like this would work. If you need this feature I can add it when I get time.

Have you looked at the Blazor Image Gallery sample project, blog post and / or video? It just dawned on me I need to update my read me here with a link to all of these.

Until I do, the sample project / blog tutorial and video information is listed below (if the answer is no to my last question).

Blazor Image Gallery Tutorial Blog Post https://datajugglerblazor.blogspot.com/2020/02/building-blazor-image-gallery-complete.html

Blazor Image Gallery Project https://github.com/DataJuggler/BlazorImageGallery

Blazor Image Gallery Video https://youtu.be/3xKXJQ4qThQ

Warning: I have already gotten negative feedback on the video due to my must be only funny to me sense of humor, because of a few 4 second censored partially nude female animated images I used for scene separators. Everyone is so easily offended these days.

If you think this project is worth the price of free, please star it here and / or consider becoming a YouTube subscriber.

Thanks, and let me know about the feature request. I keep expecting Microsoft to come out with some built in Blazor way to do some of this.

Corby

VR-Architect commented 4 years ago

Thanks Corby!

I added this code just above the FileStream you recommended and it works.

System.Text.Encoding LocalEncoding = System.Text.Encoding.UTF8;
var contents = LocalEncoding.GetString(ms.ToArray());

It would be great if the Nuget package had the ability to set a value in to handle "DownloadAFileHandler" and "GrabTextHandler". This way we could use the same control to either grab a text (like XML, CSV, etc.) to process as desired, and/or save the file to disk.

Thanks again for the excellent component. Scott

VR-Architect commented 4 years ago

Update: I had to add "UploadComplete = true;" before my return and now I see a reset button which now permits the file to be re-uploaded. However, if I set ShowResetButton to false, the control is invisible after the first upload. Is it possible to ShowResetButton=false will result in the control auto-reseting and showing the upload button again? Much thanks again.

Hey Corby, I have the same issue with the reset not working. I upload a CSV file and get the content, then run reset, then select the same file again, but nothing happens. The HandleSelection method is not called the second time.

Here is the code I added to your example:

        [Parameter] public EventCallback<string> OnGotData { get; set; }
        [Parameter] public EventCallback<UploadedFileInfo> OnFileSaved{ get; set; }
        [Parameter] public bool GetContent { get; set; }
        [Parameter] public bool SaveFileToDrive { get; set; }
...
                            // Added by Scott         
                            if (GetContent)
                            {
                                System.Text.Encoding LocalEncoding = System.Text.Encoding.UTF8;
                                var contents = LocalEncoding.GetString(ms.ToArray());
                                Reset();
                                await OnGotData.InvokeAsync(contents);
                            }
                            if (!SaveFileToDrive)
                            {
                                return;
                            }
                            // End add by Scott

On my calling razor page I have this:

<MyCompany.Web.Widgets.VFileUploader @ref="fileUploader" GetContent="true" OnGotData="GotData"></Voogu.Web.Areas.Widgets.VFileUploader>

@code
{
    protected MyCompany.Web.Widgets.VFileUploader fileUploader;
    protected void GotData(string content)
    {
        fileUploader.Reset();
    }
}
DataJuggler commented 4 years ago

I had a lot of trouble using the control at first also, that is why I moved the reset to the control.

I am stuck on one of my other projects for the rest of the day, but by tomorrow I will update my Nuget package with the new properties.

The only thing I notice that might help you until I get a chance is the way both of my samples work, I make sure the property ShowResetButton is set to true (I think it is default), and the reset button calls FileUpload.Reset, not via code after the data arrives like you did.

It is kind of annoying I know to click Upload, Click New, then having to click Upload again, but that is the way my samples are constructed and they work.

Perhaps you need to InokeAsync StateHasChanged() like this after your reset call, but that is just guessing.

// Update the UI InvokeAsync(() => { StateHasChanged(); });

If your code is something you can share, I can take a look at if you post on it Git Hub or somewhere.

Sorry it isn't working for you, I found it rather finicky when it wasn't working.

Corby

DataJuggler commented 4 years ago

I published a new version to Nuget, and also I updated the sample project located in this repository, , and I checked in all the new code.

The new features include:

UploadFileInfo.cs now contains 3 new properties

SaveToDisk bool - defaults to true

Stream MemoryStream - will only be set if SaveToDisk is false

HasStream bool - Saves testing for (String != null);

Sample project:

There is now a 3rd FileUpload button, and a simple xml file is located in the Xml folder of the sample project.

Can you let me know if this works for you please, and I will close this issue?

I am making a short video now with the new features.

Thanks for the feature idea, 99% of my code exists because I had a need for it.

Corby