I get this console error: "{ "uid": "rc-upload-1688616289733-3", "lastModified":…
- User
I get this console error: "{ "uid": "rc-upload-1688616289733-3", "lastModified": 1688616176993, "lastModifiedDate": "2023-07-06T04:02:56.993Z", "name": "statics.xlsx", "size": 8633, "type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "percent": 0, "originFileObj": { "uid": "rc-upload-1688616289733-3" }, "error": { "status": 400, "method": "post", "url": "http://localhost:5041/api/admin/AddStatic" }, "response": { "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "One or more validation errors occurred.", "status": 400, "traceId": "00-684de5adf77c1fb735a4d12fb24fb8d8-c9fa45cbd1fed588-00", "errors": { "files": [ "The files field is required." ] } }, "status": "error" }" when i post this excel file upload: "const props = { name: 'file', action: 'http://localhost:5041/api/admin/AddStatic', // change this to your server endpoint that will handle the file upload headers: { authorization: 'authorization-text', }, onChange(info) { if (info.file.status !== 'uploading') { console.log(info.file, info.fileList); } if (info.file.status === 'done') { message.success(
${info.file.name} تم رفع الملف); // save the response url in the state } else if (info.file.status === 'error') { message.error(${info.file.name} فشل رفع الملف.); } }, progress: { strokeColor: { '0%': '#108ee9', '100%': '#87d068', }, strokeWidth: 3, format: (percent) =>${parseFloat(percent.toFixed(2))}%, }, };" to this server-side controller: "[HttpPost("AddStatic")] public async Task UploadStatic(IFormFile files) { if (files != null && files.Length > 0) { using var stream = new MemoryStream(); await files.CopyToAsync(stream); stream.Position = 0;using var reader = ExcelReaderFactory.CreateReader(stream); var config = new ExcelDataSetConfiguration { ConfigureDataTable = _ => new ExcelDataTableConfiguration { UseHeaderRow = false // Assuming there are no headers in the first row } }; var dataSet = reader.AsDataSet(config); var dataTable = dataSet.Tables[0]; // Assuming there is only one sheet in the Excel file var stringsList = new List<string>(); foreach (DataRow row in dataTable.Rows) { var stringValue = row[0].ToString(); // Assuming data is in the first column stringsList.Add(stringValue); } // Add the strings to _context.StaticMsgs foreach (var str in stringsList) { _context.StaticMsgs.Add(new StaticMsg { Txt = str }); } await _context.SaveChangesAsync(); return Ok(); } return BadRequest("No file uploaded"); }" figure out the issue and fix it. - Assistant
The issue is that the server-side controller expects the file to be uploaded with the name "files", but in your client-side code, you are giving it the name "file". To fix this issue, you need to update the "name" property in your client-side code to "files". Here's the updated code:
const props = { name: 'files', action: 'http://localhost:5041/api/admin/AddStatic', // change this to your server endpoint that will handle the file upload headers: { authorization: 'authorization-text', }, onChange(info) { if (info.file.status !== 'uploading') { console.log(info.file, info.fileList); } if (info.file.status === 'done') { message.success(`${info.file.name} تم رفع الملف`); // save the response url in the state } else if (info.file.status === 'error') { message.error(`${info.file.name} فشل رفع الملف.`); } }, progress: { strokeColor: { '0%': '#108ee9', '100%': '#87d068', }, strokeWidth: 3, format: (percent) => `${parseFloat(percent.toFixed(2))}%`, }, };With this change, the file will be uploaded with the correct name "files", and the server-side controller will be able to receive it properly.