Untitled conversation
- User
[HttpGet] public IActionResult DownloadFile(string fileName, int themeId) { var fileService = _serviceProvider.GetRequiredService(); var requestedFile = fileService.DownloadFile(fileName, themeId); if (requestedFile != null) { return requestedFile; } else { return BadRequest("Не удалось скачать файл"); } } public ActionResult DownloadFile(string filename, int themeId) { var themeFile = _themeFilesService.GetFileForTheme(filename, themeId); if (themeFile == null) { return null; } string pathToFile = Path.Combine(PathToFiles, themeFile.File.GetGuidFileName());
if (System.IO.File.Exists(pathToFile)) { byte[] fileBytes = System.IO.File.ReadAllBytes(pathToFile); return new FileContentResult(fileBytes, "application/octet-stream") { FileDownloadName = themeFile.File.GetOriginalFileName() }; } else { return null; }} public ThemeFile GetFileForTheme(string filename, int themeId) { return _context.ThemeFiles.FirstOrDefault(tf => tf.File.OriginalName == filename && tf.ThemeId == themeId); } public class ThemeFile { public int Id { get; set; } public int ThemeId { get; set; } [ForeignKey(nameof(ThemeId))] public virtual Theme? Theme { get; set; } public int FileId { get; set; } [ForeignKey(nameof(FileId))] public virtual FileData? File { get; set; } } public class FileData { public int Id { get; set; } public string OriginalName { get; set; } public string Guid { get; set; } public string Extension { get; set; } public string GetGuidFileName() { return "{Guid}{Extension}"; } public string GetOriginalFileName() { return "{OriginalName}{Extension}"; } } document.querySelectorAll('.download-btn').forEach(button => { button.addEventListener('click', function () { const fileName = this.dataset.fileName; const themeId = this.dataset.themeId; downloadFile(fileName, themeId); }); });
function downloadFile(fileName, themeId) { $.ajax({ type: 'GET', url: '/Themes/DownloadFile', data: { fileName: fileName, themeId: themeId }, xhrFields: { responseType: 'blob' }, success: function (data) { const blob = new Blob([data], { type: 'application/octet-stream' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = fileName; document.body.appendChild(a); a.click(); document.body.removeChild(a); window.URL.revokeObjectURL(url); }, error: function (xhr, status, error) { console.error('Error downloading file:', error); } }); } файл скачивается, но почему-то без расширения, хотя при отладке я смотрю и в FileDownloadName попадает имя файла с расширением
- Assistant
Анализ и исправление:
1. Ключевая ошибка: В JavaScript-функции
downloadFile, вы задаете , но не включает расширение файла. Оно было задано корректно на стороне сервера ( в ), но игнорировалось на клиенте.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.