Uploading files

Can you debug to see if there are any errors when this.context.SaveChanges(); is executed?

Continuing the discussion from Uploading files:

Thank you Vladimir

When debug starts, after a while, the following crashes.

And in the browser, this is what happens.

According to the exception SQL column FileName cannot be null however it is. You need to set this property.

Please tell me how to do this, my level in programming is very low to solve such a difficult task.
thank :relieved:

Here is what @joshwilliam did:

//Save the Record as an Order Attachment.
var t = new OrderAttachment
{

    FileName = file.FileName,
    Url = url,
    OrderId = ordId
};

this.context.OrderAttachments.Add(t);
this.context.SaveChanges();

It is thanks to @joshwilliam that I am now at this stage.
But nothing works, I fight for a very long time, no strength)

Here is the upload controller code:
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Data.SqlClient;
using static Microsoft.AspNetCore.Hosting.Internal.HostingApplication;

namespace UploadFiles.Controllers
{
  using Models;
  using Data;
  using Models.Northwind;
  
    [Route("upload")]
    public partial class UploadController : Controller
    {

        private readonly IHostingEnvironment hostingEnvironment;
        private UploadFiles.Data.NorthwindContext context;

        public UploadController(IHostingEnvironment hostingEnvironment, UploadFiles.Data.NorthwindContext context)
        {
            this.hostingEnvironment = hostingEnvironment;
            this.context = context;
        }


        [HttpPost]
        public IActionResult UploadFiles(IEnumerable<IFormFile> files, int orderId)
        {
            // Combine the orderId with the WebRootPath so files are stored per orderId
            var dir = Path.Combine(hostingEnvironment.WebRootPath, orderId.ToString());
            // Create the directory if it doesn't exist
            Directory.CreateDirectory(dir);

            var result = files.Select(file =>
            {
                // Save the files in the directory
                var path = Path.Combine(dir, file.FileName);

                using (var stream = new FileStream(path, FileMode.Create))
                {
                    // Save the file
                    file.CopyTo(stream);

                    var request = HttpContext.Request;

                    // Create the URL for the uploaded file - not that orderId is part of the URL
                    var url = $"{request.Scheme}://{request.Host.Value}/{orderId}/{file.FileName}";

                    //Save the Record as an Order Attachment.
                      var t = new OrderAttachment
					{

						FileName = file.FileName,
						Url = url,
						OrderId = orderId
					};

					this.context.OrderAttachments.Add(t);
					this.context.SaveChanges();
					  
					

                // Return the FileName and Url
                return new
                {
                    fileName = file.FileName,
                    Url = url,
                    OrderId = orderId
                };      

            }
            
        }).ToList();

        // Return the file names and URL of the uploaded files
        return Json(new { value = result }, new JsonSerializerSettings()
        {
            ContractResolver = new DefaultContractResolver()
        });

    }
}
}

If you own Radzen license you can send us your project and a SQL script of your database schema at info@radzen.com and we will provide more info.

Unfortunately, I do not have a license yet, I want to understand and purchase a license.
:cry:

There is a chance we have reproduced a similar problem. Radzen fails to properly parse the custom method if the ServerMethodsController has compilation errors at some point. Try deleting the Upload handler in Radzen and adding it again.

Well thank you very much
I'll try

There is!
Earned!
But! Now, when adding files, upload processes only one file (and adds to attachments too), and the rest returns with an error of 500


Server error 500 means that there is an exception. Use Visual Studio to see what it is.

Hi,
how do you implemented filename saving? User have to enter filename into some TextBox or you are able to obtain file name somehow from FileInput component? From event it is not possible and I am interested in some other way, probably how to get it from DOM where this information is.

Pete