Regarding File Upload to sql server from front end

Hello! I was working with the School Management System project. I use Midleware as the swagger api and Blazor for the front end. I uploaded the photo using the syncfusion component , but I'm not sure how to upload it to a SQL server. Additionally, I'm using the entity framework code first method to store the data in the server. Could you maybe provide an example of how to post the photo to a SQL server?



1 Reply

KP Kokila Poovendran Syncfusion Team April 2, 2024 12:46 PM UTC

Hi Rajarajeswari,


Greetings from Syncfusion Support!


Thank you for reaching out to us regarding your query on uploading files to a SQL Server from the front end of your School Management System project. We understand the importance of seamlessly integrating Syncfusion components like the Uploader with your project.


Based on your requirements, we've crafted a simple sample to demonstrate the process. Below is the code snippet for your reference:


Index.razor

<SfUploader ID="UploadFiles" SequentialUpload=true AutoUpload=false>

    <UploaderAsyncSettings SaveUrl="api/SampleData/Save" RemoveUrl="api/SampleData/Remove">

    </UploaderAsyncSettings>

</SfUploader>




SampleDataController.cs

[HttpPost("[action]")]

public void Save(IList<IFormFile> UploadFiles)

{

// UploadFiles ->Here you can get the file information

    try

    {

        foreach (var file in UploadFiles)

        {

            var filename = hostingEnv.ContentRootPath + $@"\{file.FileName}";

            if (!System.IO.File.Exists(filename))

            {

                using (FileStream fs = System.IO.File.Create(filename))

                {

                    file.CopyTo(fs);

                    fs.Flush();

                }

            }

        }

    }

    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;

    }

}



In this code snippet, we've utilized the SaveUrl property to handle the saving of uploaded files or images. The controller (SampleDataController.cs) captures the file details, allowing you to process and store them as needed within your SQL Server environment.


Documentationhttps://blazor.syncfusion.com/documentation/file-upload/async


Loader.
Up arrow icon