|
@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;
}
} |
|
<SfUploader ID="UploadFiles" AutoUpload="true">
<UploaderEvents FileSelected="onFileSelected"></UploaderEvents>
<UploaderAsyncSettings SaveUrl="@saveUrl" RemoveUrl="@removeurl">
</UploaderAsyncSettings>
</SfUploader>
@code {
public string saveUrl { get; set; }
public string removeurl { get; set; }
protected override void OnInitialized()
{
this.saveUrl = "https://aspnetmvc.syncfusion.com/services/api/uploadbox/Save";
this.removeurl = "https://aspnetmvc.syncfusion.com/services/api/uploadbox/Remove";
}
public void onFileSelected(SelectedEventArgs e)
{
this.saveUrl = "https://localhost:44394/api/SampleData/Save";
this.removeurl = "https://localhost:44394/api/SampleData/Remove";
}
} |
I hate to resurrect an old thread, but it's for a good reason: to thank you for having this feature! Really nice work out there, Team Syncfusion. 👍
Doh, actually, I ran into a problem. I've posted a new thread here if interested:
https://www.syncfusion.com/forums/174108/refused-to-set-unsafe-header-cookie
Hi Mike,
You are most welcome. Please follow-up with the newly created thread for further assistance.
Regards,