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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Auto upload example

Thread ID:

Created:

Updated:

Platform:

Replies:

143879 Apr 8,2019 07:19 PM UTC Aug 21,2019 03:52 AM UTC ASP.NET Core - EJ 2 8
loading
Tags: File Upload
Markus
Asked On April 8, 2019 07:19 PM UTC

Do you have an example of auto upload how-to for dotnet core 2.2. razor pages?

Prince Oliver [Syncfusion]
Replied On April 9, 2019 11:05 AM UTC

Hi Markus, 

Thank you for contacting us. 

Yes, we have attached an example application for razor application to render the uploader component. By default, uploader component has autoUpload featured enabled. Kindly refer to the following code snippet. 

@{ 
    var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "https://aspnetmvc.syncfusion.com/services/api/uploadbox/Save", RemoveUrl = "https://aspnetmvc.syncfusion.com/services/api/uploadbox/Remove" }; 
} 
 
<div class="col-lg-8 control-section"> 
    <div class="control_wrapper"> 
        <ejs-uploader id="UploadFiles" dropArea=".control-fluid" asyncSettings="@asyncSettings"> 
        </ejs-uploader> 
    </div> 
</div> 

Please find the example application in the following location: 

For further reference,  

Let us know if you need any further assistance on this. 

Regards, 
Prince 


Markus
Replied On April 11, 2019 09:14 AM UTC

Thank you for your quick reply.

Regarding the SaveUrl and RemoveUrl, is the only way to use it via api?

Regards,
Markus

Prince Oliver [Syncfusion]
Replied On April 11, 2019 12:18 PM UTC

Hello Markus, 

Good day to you. 

No, we can implement save and remove handlers in the code behind of Razor pages also. Please refer the following code snippet.  

[Index.cshtml] 
 @{  
    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>  
 
  
[Startup.cs] 
public void ConfigureServices(IServiceCollection services)  
{  
    services.AddMvc().AddJsonOptions(x =>  
    {  
        x.SerializerSettings.ContractResolver = new DefaultContractResolver();  
    });  
    services.AddAntiforgery(o => o.HeaderName = "xsrf-token");  
}  
 
  
[Index.cshtml.cs] 
 [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("");  
}  
 
  
We have attached the example application for your reference, please find it in the following location: http://www.syncfusion.com/downloads/support/forum/143879/ze/Razorpages700274431 

Let us know if you need any further assistance on this. 

Regards, 
Prince 


Derek Gabriel
Replied On July 7, 2019 06:27 AM UTC

Is this example available anywhere for .net core 2.2?

Derek Gabriel
Replied On July 8, 2019 05:25 AM UTC

Figured it out, just had to make some small changes and it's working.

Had to remove the [AcceptVerbs("")] attributes, Visual Studio actually had a useful error message for this one.

Prince Oliver [Syncfusion]
Replied On July 8, 2019 05:31 AM UTC

Hi Derek,     

Thank you for your update. We are glad that the issue is resolved in your end. Let us know if you need any further assistance on this. 

Regards, 
Prince 


Michael
Replied On August 19, 2019 08:29 AM UTC

Do you think you may be able to shed some light no this for me? 
Can't get it to work using dotnet core 2.2 Razor Pages.



Narayanasamy Panneer Selvam [Syncfusion]
Replied On August 21, 2019 03:52 AM UTC

Hi Derek, 
 
Thanks for contacting us. 
 
As you have requested, we have preapred the sample using dotnet core 2.2 razor pages. 
Please find the sample from below link, 
 
Regards, 
Narayanasamy P. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon