コード例 #1
0
        public int SaveImage(FileDto fileDto)
        {
            var image = new Image
            {
                CompilationId = fileDto.CompilationId,
                Name = fileDto.Name,
                Path = fileDto.Path
            };
            _imageRepository.Add(image);
            _imageRepository.Save();

            return image.Id;
        }
コード例 #2
0
        public ActionResult UploadFiles()
        {
            var uploadedFiles = new List<UploadFileResult>();

            foreach (string postedFile in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[postedFile];
                if (file != null && file.ContentLength == 0)
                    continue;

                int savedFileId = 0;
                switch (file.ContentType)
                {
                    case "audio/mp3":
                        {
                            string path = Path.Combine(Server.MapPath("~/App_Data/Tracks"), Path.GetFileName(file.FileName));
                            file.SaveAs(path);

                            var fileDto = new FileDto
                            {
                                Name = file.FileName,
                                Path = path
                            };
                            savedFileId = _tracksService.SaveTrack(fileDto);
                            break;
                        }
                    case "image/jpeg":
                    case "image/png":
                        {
                            string path;
                            var fileDto = new FileDto();
                            if (file.FileName == "blob")
                            {
                                string fileName = string.Format("{0}.jpg", Guid.NewGuid());
                                path = Path.Combine(Server.MapPath("~/App_Data/Images"), fileName);
                                fileDto.Name = fileName;
                                fileDto.Path = path;
                            }
                            else
                            {
                                path = Path.Combine(Server.MapPath("~/App_Data/Images"), Path.GetFileName(file.FileName));
                                fileDto.Name = file.FileName;
                                fileDto.Path = path;
                            }
                            file.SaveAs(path);

                            savedFileId = _imageService.SaveImage(fileDto);
                            break;
                        }
                }

                uploadedFiles.Add(new UploadFileResult()
                {
                    Id = savedFileId,
                    Name = file.FileName,
                    Length = file.ContentLength,
                    Type = file.ContentType
                });
            }

            return Json(uploadedFiles);
        }