We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

SfUploader - get full path of file being uploaded

Syncfusion.Blazor.Inputs version 21.1.41

I'm trying to create FileStreams of the uploaded files.
I need the full path to the file, but FileInfo.Name contains filename only.
What am I doing wrong?

<SfUploader AutoUpload="true" DropArea="false" AllowedExtensions=".txt, .csv, .iif" MinFileSize=3 MaxFileSize=40857600>
        <UploaderEvents ValueChange="OnFileUpload"></UploaderEvents>
</SfUploader>


public void OnFileUpload(Syncfusion.Blazor.Inputs.UploadChangeEventArgs args)
{
  FileStream fs;
  foreach (var file in args.Files)
  {
    fs = new FileStream(file.FileInfo.Name, FileMode.Create);
   }
}




4 Replies 1 reply marked as answer

AS Aaron Smith April 23, 2023 12:38 AM UTC

The access to the file stream changed a bit recently. Here's a full working example:


@page "/"

@using Syncfusion.Blazor.Inputs
@using System.IO
@inject IJSRuntime JsRuntime

<SfUploader @ref="fileUploader" AutoUpload="true" MaxFileSize="500000000">
<UploaderEvents ValueChange="OnUploadChanged" Success="OnUploadSuccess" OnActionComplete="OnActionComplete"></UploaderEvents>
</SfUploader>

@code {

private SfUploader uploderObj;

public async Task OnUploadSuccess(SuccessEventArgs args)
{
await LogAsync($"Success: {args.File.Name}");
}

public async Task OnActionComplete(ActionCompleteEventArgs args)
{
await LogAsync($"OnActionComplete file count: {args.FileData.Count}");

if(args.FileData.Count > 0)
{
await uploderObj.ClearAllAsync();
}
}

public async Task OnUploadChanged(UploadChangeEventArgs args)
{
foreach (var file in args.Files)
{
await LogAsync($"OnUploadChanged: {file.File.Name}");
var path = Path.GetFullPath("wwwroot\\uploads\\") + file.FileInfo.Name;
FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);
await file.File.OpenReadStream(long.MaxValue).CopyToAsync(filestream);
filestream.Close();
}

//remove this file, or it will turn up the next time OnUploadChanged fires
args.Files.Remove(args.Files.First());

}

public async Task LogAsync(string message)
{
await JsRuntime.InvokeVoidAsync("console.log", message);
}
}

Marked as answer

DA David April 23, 2023 11:16 PM UTC

Thanks Aaron, that helped a lot. I was able to create my own solution based on the sample code..

BTW, the sample won't compile because @ref="fileUploader" points to a non-existent variable.



AS Aaron Smith April 24, 2023 08:49 AM UTC

Ah yes, my markup was edited in later, must have used the wrong example!



PK Priyanka Karthikeyan Syncfusion Team April 24, 2023 09:40 AM UTC

Hi David,


You need to use the same name for both the @ref attribute and the SfUploader variable name, which in this case is 'uploaderObj'. Please find the example code and sample for your reference.


<SfUploader @ref="uploderObj" AutoUpload="true" MaxFileSize="500000000">

    <UploaderEvents ValueChange="OnUploadChanged" Success="OnUploadSuccess" OnActionComplete="OnActionComplete"></UploaderEvents>

</SfUploader>

 

@code {

 

    private SfUploader uploderObj;


Regards,

Priyanka K


Attachment: FileUpload_6d630ce6.zip

Loader.
Live Chat Icon For mobile
Up arrow icon