|
@{
var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "http://localhost:50707/Index?handler=Save", RemoveUrl = "http://localhost:50707/Index?handler=Remove" };
}
@Html.AntiForgeryToken()
<ejs-uploader id="uploadFiles" asyncSettings="@asyncSettings" multiple="false" autoUpload="false" uploading="addTokens"></ejs-uploader>
<script>
function addTokens(args) {
args.currentRequest.setRequestHeader('XSRF-TOKEN', document.getElementsByName('__RequestVerificationToken')[0].value);
}
</script> |
|
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(x =>
{
x.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddAntiforgery(o => o.HeaderName = "xsrf-token");
}
|
|
[AcceptVerbs("Post")]
public IActionResult OnPostSave(IList<IFormFile> UploadFiles)
{
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.";
}
}
}
}
catch (Exception e)
{
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "No Content";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
return Content("");
}
[AcceptVerbs("Post")]
public IActionResult OnPostRemove(IList<IFormFile> UploadFiles)
{
try
{
foreach (var file in UploadFiles)
{
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var filePath = Path.Combine(hostingEnv.WebRootPath);
var fileSavePath = filePath + "\\" + fileName;
if (!System.IO.File.Exists(fileSavePath))
{
System.IO.File.Delete(fileSavePath);
}
}
}
catch (Exception e)
{
Response.Clear();
Response.StatusCode = 200;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File removed successfully";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
return Content("");
}
|
hi..
I got a 400 error when i post 「Remove handler」 (however,「Save handler」 is ok).
Remove code is below:
[AcceptVerbs("Post")]
public IActionResult OnPostRemove(IList<IFormFile> UploadFiles)
{
try
{
foreach (var file in UploadFiles)
{
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var filePath = Path.Combine(_hostingEnvironment.WebRootPath);
var fileSavePath = @filePath + "\\UpdFiles\\" + fileName;
if (!System.IO.File.Exists(fileSavePath))
{
System.IO.File.Delete(fileSavePath);
}
}
}
catch (Exception e)
{
Response.Clear();
Response.StatusCode = 200;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File removed successfully";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
return Content("");
//return RedirectToPage("UploadFiles");
}
cshtml file is below:@page
@using Microsoft.Net.Http.Headers
@model Razor.Pages.TestZone.UploadFilesModel
@{
var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "UploadFiles?handler=Save", RemoveUrl = "UploadFiles?handler=Remove", ChunkSize = 3002400 };
}
@Html.AntiForgeryToken()
<ejs-uploader id="uploadFiles" asyncSettings="@asyncSettings" autoUpload="false" uploading="addTokens" maxFileSize="3024000" allowedExtensions=".doc, .docx, .xls, .xlsx, .jpg, .jpeg, .png, .pdf" locale="tw-ZH"></ejs-uploader>
<script>
function addTokens(args) {
args.currentRequest.setRequestHeader('XSRF-TOKEN', document.getElementsByName('__RequestVerificationToken')[0].value);
}
</script>
We will validate the requirement in our component. We update you in two business days (May 5th, 2022).
We are still validating your requirement , We will update the details within two business days (May 10th 2022).
Based on your provided code example, the Tokens are not set in the remove
request header. So we suggest you add the same tokens
by using the BeforeRemove or OnRemove events to achieve
your requirement.
Hi..
Do you have any demo code that can work for me to verify delete function, thx a lots.
Hi Kuo,
Please find the attached sample as per your requirement . We have added request headers in uploading and removing event of the uploader.
[Index.cshtml]
|
@Html.AntiForgeryToken() <div class="upload-wrapper"> <ejs-uploader id="uploadFiles" asyncSettings="@asyncSettings" multiple="false" autoUpload="true" uploading="addTokens" removing="addTokens"></ejs-uploader> </div>
<script> function addTokens(args) { console.log(args) args.currentRequest.setRequestHeader('XSRF-TOKEN', document.getElementsByName('__RequestVerificationToken')[0].value); }
</script> |
Thanks,
Deepak R.
Olumuyiwa,
Thanks for your update.
Regards,
Sureshkumar P