示例#1
0
        public async Task <IActionResult> UploadPhotos(List <IFormFile> files, List <int> priorities, int itemId)
        {
            var models = new List <PhotoModel>();

            for (var i = 0; i < files.Count; i++)
            {
                IFormFile file = files[i];
                if (_photoUploadService.CheckFile(file.OpenReadStream(), file.ContentType, file.Length))
                {
                    var fileName       = _photoUploadService.GenerateFileName(file.FileName);
                    var itemPhotosPath = $"\\itemPhotos\\{itemId}\\{fileName}";
                    var filePath       = String.Concat(_photoUploadPath, itemPhotosPath);
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    }
                    using (Stream stream = new FileStream(filePath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }
                    models.Add(new PhotoModel
                    {
                        Path     = itemPhotosPath,
                        Priority = priorities[i],
                        ItemId   = itemId
                    });
                    TransformPhoto(filePath, fileName);
                }
            }

            List <OperationResult <PhotoModel, Photo> > operationResult = new List <OperationResult <PhotoModel, Photo> >();

            models.ForEach(m => operationResult.Add(PerformOperation(m, OperationType.Add)));
            return(Json(operationResult));
        }
示例#2
0
 private async Task <string> WriteFile(IFormFile file)
 {
     if (_photoUploadService.CheckFile(file.OpenReadStream(), file.ContentType, file.Length))
     {
         var fileName = _photoUploadService.GenerateFileName(file.FileName);
         var filePath = String.Concat(_webrootPath, "MainPage/", fileName);
         using (Stream stream = new FileStream(filePath, FileMode.Create))
         {
             await file.CopyToAsync(stream);
         }
         return(fileName);
     }
     return(null);
 }