Beispiel #1
0
        public IHttpActionResult uploadPhoto(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;


                // Todo: Check if length of httpRequest.Files <= 10 to ensure only 10 photos is uploaded

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

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

                var count = 0;

                for (int i = 0; i < httpRequest.Files.Count; i++)
                {
                    // Grab current file of the request
                    //var putFile = httpRequest.Files[filename];
                    var putFile = httpRequest.Files[i];

                    // 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 .jpg only");
                            // Todo: Log the error that occurs
                            return(BadRequest("Please upload image of type .jpg only"));
                        }
                        // 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("Please upload a file upto 1mb"));
                        }
                        // Send to production service where functinality to save the file is
                        else
                        {
                            productionService.UploadPhoto(productionId, count, extension, putFile);
                        }
                    }

                    count++;
                }

                return(Ok("Photo Uploaded"));
            }

            catch (Exception ex)
            {
                // Todo: add proper error handling
                // Todo: log error
                return(BadRequest("Photo could not be uploaded...dont know why.find out and add detailed message here!"));
            }
        }