Exemplo n.º 1
0
        public JsonResult UploadFile(AddFile model)
        {
            if (model == null)
            {
                return(null);
            }

            return(this.Json(this._fileService.UploadFiles(model, this.Request)));
        }
        public JsonResult UploadFile(AddFile model)
        {
            if (model == null)
            {
                return null;
            }

            return this.Json(this._fileService.UploadFiles(model, this.Request));
        }
Exemplo n.º 3
0
        private UploadFileResult GetUploadResult(File uploadedFile, AddFile model)
        {
            var result = new UploadFileResult();

            if (uploadedFile != null)
            {
                result.Succeeded = true;
                result.FileId    = uploadedFile.Id;
                int height     = model.DisplayHeight > 0 ? model.DisplayHeight : model.MaxHeight;
                int width      = model.DisplayWidth > 0 ? model.DisplayWidth : model.MaxWidth;
                var resultPath = string.Format("{0}.{1}", System.IO.Path.Combine(uploadedFile.Folder, uploadedFile.Path, uploadedFile.FileName), uploadedFile.Extension);
                result.Image        = this._imageConverter.ImageAsBase64(resultPath, width, height, model.KeepAspectRatio);
                result.Message      = Resources.Interface.FileUploadSuccess;
                result.DocumentType = this._fileManager.GetDocumentType(uploadedFile.Extension);
                result.Extension    = uploadedFile.Extension;
            }
            else
            {
                result.Message = Resources.Interface.ErrorUploadingFile;
            }

            return(result);
        }
Exemplo n.º 4
0
        public IList <UploadFileResult> UploadFiles(AddFile model, HttpRequestBase request)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var result  = new List <UploadFileResult>();
            var allowed = true;

            // first validate them all
            foreach (string fileName in request.Files)
            {
                var file = request.Files[fileName];

                // Todo: test for zip.
                if (!this._fileManager.IsFileAllowed(file.FileName, model.Unzip ? new string[] { "zip" } : null))
                {
                    allowed = false;
                    break;
                }
            }

            if (allowed)
            {
                // then save them
                try
                {
                    foreach (string fileName in request.Files)
                    {
                        var file = request.Files[fileName];
                        System.IO.Stream fileStream = file.InputStream;
                        byte[]           fileData   = new byte[file.ContentLength];
                        fileStream.Read(fileData, 0, file.ContentLength);
                        var args = new SaveFileArguments {
                            FileData = fileData, CustomX = model.CustomWidth, CustomY = model.CustomHeight, MaxX = model.MaxWidth, MaxY = model.MaxHeight
                        };

                        if (model.Unzip && file.FileName.ToLower().EndsWith(".zip"))
                        {
                            var files = this._fileManager.SaveMany(args);

                            foreach (var theFile in files)
                            {
                                result.Add(this.GetUploadResult(theFile, model));
                            }
                        }
                        else
                        {
                            args.FileName = file.FileName;
                            var theFile = this._fileManager.Save(args);
                            result.Add(this.GetUploadResult(theFile, model));
                        }
                    }

                    this._dataSource.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.Message, ex, LogLevel.Fatal);
                    throw;
                }
            }

            return(result);
        }
        private UploadFileResult GetUploadResult(File uploadedFile, AddFile model)
        {
            var result = new UploadFileResult();

            if (uploadedFile != null)
            {
                result.Succeeded = true;
                result.FileId = uploadedFile.Id;
                int height = model.DisplayHeight > 0 ? model.DisplayHeight : model.MaxHeight;
                int width = model.DisplayWidth > 0 ? model.DisplayWidth : model.MaxWidth;
                var resultPath = string.Format("{0}.{1}", System.IO.Path.Combine(uploadedFile.Folder, uploadedFile.Path, uploadedFile.FileName), uploadedFile.Extension);
                result.Image = this._imageConverter.ImageAsBase64(resultPath, width, height, model.KeepAspectRatio);
                result.Message = Resources.Interface.FileUploadSuccess;
                result.DocumentType = this._fileManager.GetDocumentType(uploadedFile.Extension);
                result.Extension = uploadedFile.Extension;
            }
            else
            {
                result.Message = Resources.Interface.ErrorUploadingFile;
            }

            return result;
        }
        public IList<UploadFileResult> UploadFiles(AddFile model, HttpRequestBase request)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var result = new List<UploadFileResult>();
            var allowed = true;

            // first validate them all
            foreach (string fileName in request.Files)
            {
                var file = request.Files[fileName];

                // Todo: test for zip.
                if (!this._fileManager.IsFileAllowed(file.FileName, model.Unzip ? new string[] { "zip" } : null))
                {
                    allowed = false;
                    break;
                }
            }

            if (allowed)
            {
                // then save them
                try
                {
                    foreach (string fileName in request.Files)
                    {
                        var file = request.Files[fileName];
                        System.IO.Stream fileStream = file.InputStream;
                        byte[] fileData = new byte[file.ContentLength];
                        fileStream.Read(fileData, 0, file.ContentLength);
                        var args = new SaveFileArguments { FileData = fileData, CustomX = model.CustomWidth, CustomY = model.CustomHeight, MaxX = model.MaxWidth, MaxY = model.MaxHeight };

                        if (model.Unzip && file.FileName.ToLower().EndsWith(".zip"))
                        {
                            var files = this._fileManager.SaveMany(args);

                            foreach (var theFile in files)
                            {
                                result.Add(this.GetUploadResult(theFile, model));
                            }
                        }
                        else
                        {
                            args.FileName = file.FileName;
                            var theFile = this._fileManager.Save(args);
                            result.Add(this.GetUploadResult(theFile, model));
                        }
                    }

                    this._dataSource.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.Message, ex, LogLevel.Fatal);
                    throw;
                }
            }

            return result;
        }