Exemplo n.º 1
0
        public IHttpActionResult UploadProductionProgram(int productionId)
        {
            var dbcontext         = new BroadwayBuilderContext();
            var productionService = new ProductionService(dbcontext);

            //try to upload pdf and save to server filesystem
            try
            {
                //get the content, headers, etc the full request of the current http request
                var httpRequest = HttpContext.Current.Request;

                //A list in case we want to accept more than one file type
                IList <string> AllowedFileExtension = new List <string> {
                    ".pdf"
                };

                // Todo: Check if length of httpRequest.Files == 1 to ensure only 1 file is uploaded

                // Max file size is 1MB
                int MaxContentLength = 1024 * 1024 * 1;

                foreach (string filename in httpRequest.Files)
                {
                    // Grab current file of the request
                    var putFile = httpRequest.Files[filename];

                    // Continue if the file has content
                    if (putFile != null && putFile.ContentLength > 0)
                    {
                        // Checks the current extension of the current file
                        var ext       = putFile.FileName.Substring(putFile.FileName.LastIndexOf('.'));
                        var extension = ext.ToLower();

                        // File extension is not valid
                        if (!AllowedFileExtension.Contains(extension))
                        {
                            //var message = string.Format("Please Upload image of type .pdf only");
                            // Todo: Log the error that occurs
                            return(BadRequest("File needs to be of type pdf"));
                        }
                        // File size is too big
                        else if (putFile.ContentLength > MaxContentLength)
                        {
                            //var message = string.Format("Please Upload a file upto 1 mb.");
                            // Todo: log the error that occurs
                            return(BadRequest("File exceeds max limit of 1 MB"));
                        }
                        // Send to production service where functinality to save the file is
                        else
                        {
                            // check if id is null or not then proceed
                            productionService.UploadProgram(productionId, extension, putFile);
                        }
                    }

                    // Todo: Create an ErrorMessage model
                    //var message1 = string.Format("Image Updated Successfully.");
                    //return Created(insert path);
                    //return Created("C:\\Users\\ProductionPrograms");
                    return(Ok("Pdf Uploaded"));
                }
                // Todo: Create an ErrorMessage model
                //var res = string.Format("Please Upload an image.");
                // Todo: log the error that occurs
                return(BadRequest("Please upload an image"));
            }
            catch (Exception ex) {
                // Todo: add proper error handling
                // Todo: log error
                return(BadRequest("Was not able to upload the image"));
            }
        }