Esempio n. 1
0
        public async Task<ActionResult> UploadFile()
        {
            string fileName = "";
            var userId = AbpSession.GetUserId();

            try
            {
                foreach (HttpPostedFileBase file in Request.Files.Cast<string>().Select(fileItem => Request.Files[fileItem]).Where(file => file != null))
                {
                    fileName = file.FileName;
                    var fileSize = file.ContentLength;

                    // check to make sure we actually have a file
                    if (file.ContentLength <= 0) throw new UserFriendlyException("I'm sorry, there was an error saving the file.");

                    // read in the file into a byte array
                    byte[] binaryFile;
                    using (var binaryReady = new BinaryReader(file.InputStream))
                    {
                        binaryFile = binaryReady.ReadBytes(file.ContentLength);
                    }

                    var fullPath = FileHelpers.GetPath(fileName, Server.MapPath(@"\App_Data\"), userId);
                    var saveFile = new File
                    {
                        FileName = fileName, // don't save the file name with the extension
                        FileSize = FileHelpers.BytesToMegaBytes(fileSize),
                        FileType = file.ContentType,
                        FilePath = fullPath,
                        UploadDateTime = DateTime.Now,
                        IsImage = FileHelpers.IsImage(file.ContentType),
                        UserId = userId
                    };

                    // go back to the main UI thread and do whatever until we are ready
                    await FileHelpers.EncryptFileToDiskAsync(binaryFile, fullPath, "zxcvbgfdsaqwert54321");
                    await _fileService.SaveFileAsync(saveFile);
                }
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return Json(new {message = fileName});

        }
Esempio n. 2
0
        public async Task<ActionResult> SaveEditedImage(ImageEditorViewModel imageEditorViewModel)
        {
            var userId = AbpSession.GetUserId();
            // continue on with whatever we need to do until we get this file and are ready to do something with it
            var fileTask = _fileService.GetFileByIdAsync(imageEditorViewModel.FileId);

            // make a regular expression to get the type and image data quickly
            var imageRegex = Regex.Match(imageEditorViewModel.DataUrl, @"data:image/(?<type>.+?),(?<data>.+)");
            var imageType = imageRegex.Groups["type"].Value.Split(';')[0];
            var base64Data = imageRegex.Groups["data"].Value;
            var binaryData = Convert.FromBase64String(base64Data);
            var size = binaryData.Length;
            string path;

            // we have done all the work we can do without requiring the file, no await till that task is done
            var file = await fileTask;
            // saving as a new file
            if (imageEditorViewModel.FileId == 0)
            {
                imageEditorViewModel.FileName += "." + imageType;
                path = FileHelpers.GetPath(imageEditorViewModel.FileName, Server.MapPath(@"\App_Data\"), userId);
            }
            else
            {
                path = file.FilePath;
            }

            await FileHelpers.EncryptFileToDiskAsync(binaryData, path, "zxcvbgfdsaqwert54321");

            // go ahead and return if we are not making a new file
            if (file.Id != 0) return Json(new {fileName = file.FileName});

            var newFile = new File
            {
                FileName = imageEditorViewModel.FileName,
                FilePath = path,
                FileSize = FileHelpers.BytesToMegaBytes(size),
                UploadDateTime = DateTime.Now,
                FileType = "image/" + imageType,
                IsImage = true,
                UserId = userId
            };

            await _fileService.SaveFileAsync(newFile);
            return Json(new {fileName = imageEditorViewModel.FileName});
        }