Multiple file upload to mssql examples

Hello,

Anyone have an example application they can share? I'm looking to upload multiple files (pdf, excel, images etc) to a tracking application I'm building.

Looking to find an example of this:

are you wanting to upload the files to be stored in a directory on the webserver or to insert into your db?

there's pros and con's to both

here's my controller class that inserts a document into the db

{
    public class InsertController : Controller
    {
        private IActionResult UploadedFile;
        private readonly EcosysContext dbContext;
        private readonly IHttpContextAccessor HttpContext;
        private readonly GlobalsService GlobalsService;
        private readonly UserManager<ApplicationUser> _userManager;

        public InsertController(EcosysContext context, IHttpContextAccessor httpContext, GlobalsService globalsService,UserManager<ApplicationUser>userManager)
        {
            this.dbContext = context;
            this.HttpContext = httpContext;
            this.GlobalsService = globalsService;
            this._userManager = userManager;

        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost("insert/single/{ClientID}/{DocumentSourceID}")]
        public async Task<IActionResult> SingleAsync(IFormFile file, int ClientID, int DocumentSourceID)

        {
            try
            {
                // Put your code here
                UploadedFile = await databaseFilePut(file, ClientID, DocumentSourceID);
                return Ok(new { UploadedFile });
            }
            catch (Exception ex)
            {
                return StatusCode(500, ex.Message);
            }
        }

        public async Task<IActionResult> databaseFilePut(IFormFile file, int ClientID, int sourceID)
        {
            byte[] filebytes;

            var stream = new MemoryStream();
            file.CopyTo(stream);

            MemoryStream ms = new MemoryStream();
            file.CopyTo(ms);
            filebytes = ms.ToArray();
            string s = Convert.ToBase64String(filebytes);


            var _user = HttpContext.HttpContext.User.Identity.Name;

            var oid = await _userManager.GetUserAsync(User);

            var _addDoc = new TblDocument();
            _addDoc.Document = s;
            _addDoc.DocumentName = file.FileName;
            _addDoc.DocType = file.ContentType;
            _addDoc.DocumentSourceID = sourceID;
            _addDoc.OwnerID = oid.OwnerID;
            _addDoc.Created = DateTime.Now;
            _addDoc.CreatedBy = _user;

            dbContext.TblDocuments.Add(_addDoc);
            try
            {
                dbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                return null;
            }


            //only add to the intermediate table if there is a real client otherwise this is a internal document
            if (ClientID > -1)
            {
                var _clientDoc = new TblClientDocument();
                _clientDoc.ClientID = ClientID;
                _clientDoc.DocumentID = _addDoc.DocumentID;

                dbContext.TblClientDocuments.Add(_clientDoc);
                dbContext.SaveChanges();
            }

            return Ok();
        }
    }

}
1 Like

Not sure which is the best approach, I'm working on trying to figure out uploading now.

I have a form that is filled out (already a record in the database) so I have it's ID. This form will have supporting documentation attached.

I would like something similar to this:

Adding the attachment should upload to the DB or filesystem and a record created that populates the grid so the attachment can be downloaded in the future.

It would be awesome if there was a sample project that demonstrates this functionality.

I created a sample application that shows how to upload files in Radzen and allows the user to download or delete them. Get it from here: radzen-examples/UploadFilesBlazor at master ยท radzenhq/radzen-examples ยท GitHub

Thank you for providing the sample, it's exactly what I was looking for.

I'm proud to be a Radzen Professional Customer, thank you again for your excellent support!

I noticed in your sample application that when you attempt delete, only the record in the database table is removed. Should the delete action also delete the file in the directory?

There is additional code which should delete the file.

Found why it does not work.

This:

var fileName = Path.Combine(HostEnvironment.ContentRootPath, name);

Should be

var fileName = Path.Combine(HostEnvironment.WebRootPath, name);

will fix the demo.

That resolved the issue.

I was able to implement in my project but have onw small issue:

  await service.CreatePafAttachment(new Models.Sssql01.PafAttachment { Name = name, Path = path });

I get the following error when I attempt to run it:

error CS4032: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<IActionResult>'

If I remove the "await" it run as as it should, what did I miss?

You've probably missed the signature of the controller method: radzen-examples/UploadController.cs at master ยท radzenhq/radzen-examples ยท GitHub

That was it thanks for you help!