BoldSignA modern eSignature application with affordable pricing. Sign up today for unlimited document usage!
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);
}
}
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);
}
}
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.
Ah yes, my markup was edited in later, must have used the wrong example!
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