0

I have done some research, and I'm confused. My files(images and videos) are stored in a folder called 'Resources/Course Media' in my Web API's project directories, and the path to these files are stored in a SQL Server Database. I need to return course data and a list of media files to display in an Angular application.

Here is the Course DTO class that should contain all the data necessary on request from Angular

public class CoursesDTO
    {
        public int Id { get; set; }
        public string CourseNumber { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string TargetGroup { get; set; }
        public string Duration { get; set; }
        public decimal Price { get; set; }
        public string PartNumber { get; set; }
        //LIST OF MEDIA FILES
    }

Here is the code I use to receive uploaded course data and files and store it in the database and local folder.

public class CoursesBL
    {
        private Context _context;

        public CoursesBL(Context context) => _context = context;

        public void Create(IFormCollection form)
        {
            try
            {
                Course course = new Course()
                {
                    Name = form["name"],
                    CourseNumber = form["courseNumber"],
                    Description = form["description"],
                    Duration = form["duration"],
                    Price = Decimal.Parse(form["price"]),
                    PartNumber = form["partNumber"],
                    TargetGroup = form["targetGroup"]
                };

                _context.Courses.Add(course);

                foreach (var file in form.Files)
                {
                    var folderName = Path.Combine("Resources", "Course Media");
                    var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

                    if (file.Length > 0)
                    {
                        var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.ToString().Trim('"');
                        var fullPath = Path.Combine(pathToSave, fileName);
                        var dbPath = Path.Combine(folderName, fileName);

                        using (var stream = new FileStream(fullPath, FileMode.Create))
                        {
                            file.CopyTo(stream);
                        }

                        _context.CourseMedia.Add(new CourseMedium
                        {
                            FileName = dbPath,
                            Title = file.Name,
                            Course = course
                        });
                    }
                    else
                    {
                        throw new Exception("BadRequest");
                    }
                }

                _context.SaveChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }

Any help will be appreciated.

1 Answer 1

1

You can only make request to get one file as HTTP response can contain at most 1 content.

To solve your problem , what you can do is - return file paths and for each file you can make individual request to get the content response to be shown in angular app.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Will I be able to make multiple requests without any problems?
Yes you can do that and since there will be asynchronous calls , user will get data as they loads.Also user won't be viewing all the files at once , so it will appear as it gets loaded.
An upvote will be appreciated if given answer helped you :)
Just for others with the same problem, if you use this method for videos, I used *ngIf="mediaURL !== undefined" on the video source, this will prevent problems from occurring when the video takes a while to load

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.