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. Image for the cookie policy date

Auto upload example

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

8 Replies

PO Prince Oliver Syncfusion Team 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 



MA Markus 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


PO Prince Oliver Syncfusion Team 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 



DG Derek Gabriel July 7, 2019 06:27 AM UTC

Is this example available anywhere for .net core 2.2?


DG Derek Gabriel 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.


PO Prince Oliver Syncfusion Team 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 



MI Michael 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.




NP Narayanasamy Panneer Selvam Syncfusion Team 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. 


Loader.
Live Chat Icon For mobile
Up arrow icon