|
public async Task OnChange(UploadChangeEventArgs args)
{
foreach (var file in args.Files)
{
var path = @"D:\" + file.FileInfo.Name;
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Instantiate the Excel application object
IApplication application = excelEngine.Excel;
//Set the default application version
application.DefaultVersion = ExcelVersion.Xlsx;
//Load the existing Excel workbook into IWorkbook
FileStream filestream = new FileStream(path, FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(filestream);
Console.WriteLine("WorkSheetCount" + workbook.Worksheets.Count);
//Get the first worksheet in the workbook into IWorksheet
IWorksheet worksheet = workbook.Worksheets[0];
filestream.Close();
file.Stream.Close();
}
}
await Task.CompletedTask;
}
|
[HttpPost("[action]")]
public async void Save(IList<IFormFile> chunkFile, IList<IFormFile> UploadFiles)
{
long size = 0;
try
{
// for chunk-upload
foreach (var file in chunkFile)
{
var filename = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
filename = hostingEnv.WebRootPath + $@"\{filename}";
size += file.Length;
if (!System.IO.File.Exists(filename))
{
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
}
}
else
{
using (FileStream fs = System.IO.File.Open(filename, FileMode.Append))
{
file.CopyTo(fs);
fs.Flush();
}
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Instantiate the Excel application object
IApplication application = excelEngine.Excel;
//Set the default application version
application.DefaultVersion = ExcelVersion.Xlsx;
//Load the existing Excel workbook into IWorkbook
FileStream filestream = new FileStream(filename, FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(filestream);
//Get the first worksheet in the workbook into IWorksheet
IWorksheet worksheet = workbook.Worksheets[0];
filestream.Close();
}
}
}
catch (Exception e)
{
Response.Clear();
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
}
|
|
Hi Sureshkumar,
@using Syncfusion.Blazor.Inputs
<SfUploader ID="UploadFiles">
<UploaderEvents FileSelected="onFileSelect"></UploaderEvents>
<UploaderAsyncSettings SaveUrl="api/SampleData/Save">
</UploaderAsyncSettings>
</SfUploader>
@code {
private void onFileSelect(SelectedEventArgs args)
{
var accessToken = "Basic test123";
args.CurrentRequest = new List<object> { new { Authorization = accessToken } };
}
} |
[SampleDataController.cs]
[HttpPost("[action]")]
public async void Save(IList<IFormFile> UploadFiles)
{
//to get authorization Header to handle save file on server side
var authorizationHeader = Request.Headers["Authorization"];
try
{
foreach (var file in UploadFiles)
{
if (UploadFiles != null)
{
var filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
filename = hostingEnv.WebRootPath + $@"\{filename}";
if (!System.IO.File.Exists(filename))
{
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
}
}
else
{
Response.Clear();
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File already exists.";
}
}
}
Response.Headers.Add("ID", "Failure"); // Assign the custom data in the response header.
}
catch (Exception e)
{
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
} |
Dear all,
the problem Eric ran into is still not solved (as of 20.4). JWT Authentication is not automatically passed during "Save" or "Delete" by the <SfUploader> component.
To get it working, you need to read the .NET auth-token:
@inject Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider TokenProvider
<SfUploader ID="FileUpload" AllowedExtensions=".xls, .xlsx, .csv" AllowMultiple="false" AutoUpload="false">
<UploaderEvents FileSelected="onFileSelect"></UploaderEvents>
<UploaderAsyncSettings SaveUrl="api/UCTUpload/save" RemoveUrl="api/UCTUpload/delete"></UploaderAsyncSettings>
</SfUploader>
@code {
private async Task onFileSelect(SelectedEventArgs args)
{
var accessTokenResult = await TokenProvider.RequestAccessToken();
if (accessTokenResult.TryGetToken(out var token))
{
args.CurrentRequest = new List<object> { new { Authorization = "Bearer " + token.Value } };
}
}
}
Thus, you don't need to make the controller [AllowAnonymous] and handle the Authorization-Header manually as in Christopher's example above.
TBH, this should be handled by the <SfUploader> component.
Regards,
Christoph
You can refer to the below Syncfusion forum regarding your requirement.