Beispiel #1
0
        private void LoadSpreadsheet()
        {
            if (!this.IsImportCancelled)
            {
                LogImportMessage("Loading Spreadsheet", 0);

                RaiseImportProgressChangedEvent("Loading Spreadsheet");

                using (var epmLiveFileStore = new EPMLiveFileStore(_spWeb))
                {
                    using (Stream stream = epmLiveFileStore.GetStream(_fileId))
                    {
                        if (!this.IsImportCancelled)
                        {
                            ParseExcelResources(stream);
                        }
                    }

                    epmLiveFileStore.Delete(_fileId);
                }

                if (!this.IsImportCancelled)
                {
                    BuildResourceTable();
                }

                _totalRecords = _dtResources.Rows.Count;
            }
        }
        // Public Methods (1) 

        public bool Export(out string file, out string message)
        {
            file    = string.Empty;
            message = string.Empty;

            try
            {
                SPList resourcePool = _spWeb.Lists["Resources"];

                ValidateAccess();

                Dictionary <string, object[]> fieldDictionary = BuildFieldDictionary(resourcePool);
                DataTable dataTable = GetResources(resourcePool);

                using (MemoryStream memoryStream = CreateSpreadsheet(fieldDictionary, dataTable))
                {
                    using (var epmLiveFileStore = new EPMLiveFileStore(_spWeb))
                    {
                        file = epmLiveFileStore.Add(memoryStream.ToArray());
                    }
                }

                return(true);
            }
            catch (Exception exception)
            {
                message = exception.Message;
                return(false);
            }
        }
        private byte[] ResizeImage(string resizeInfo)
        {
            string[] picInfo = resizeInfo.Split('|');

            int width        = int.Parse(picInfo[0]);
            int height       = int.Parse(picInfo[1]);
            int targetWidth  = int.Parse(picInfo[2]);
            int targetHeight = int.Parse(picInfo[3]);
            int x            = int.Parse(picInfo[4]);
            int y            = int.Parse(picInfo[5]);

            using (var fileStore = new EPMLiveFileStore(Web))
            {
                using (var sourceImage = new Bitmap(fileStore.GetStream(FileNameField.Value)))
                {
                    using (var bitmap = new Bitmap(width, height))
                    {
                        using (var graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.CompositingQuality = CompositingQuality.HighQuality;
                            graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                            graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                            graphics.SmoothingMode      = SmoothingMode.HighQuality;

                            graphics.DrawImage(sourceImage, new Rectangle(0, 0, width, height));

                            using (var memoryStream = new MemoryStream())
                            {
                                bitmap.Save(memoryStream, ImageFormat.Png);

                                using (var bmp = new Bitmap(bitmap))
                                {
                                    using (var pic = bmp.Clone(new Rectangle(x, y, targetWidth, targetHeight),
                                                               bitmap.PixelFormat))
                                    {
                                        using (var stream = new MemoryStream())
                                        {
                                            pic.Save(stream, ImageFormat.Png);
                                            return(stream.ToArray());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        protected void UploadPictureButton_Click(object sender, EventArgs e)
        {
            if (!PictureFileUpload.HasFile)
            {
                return;
            }

            using (var fileStore = new EPMLiveFileStore(Web))
            {
                FileNameField.Value = fileStore.Add(PictureFileUpload.FileBytes,
                                                    Path.GetExtension(PictureFileUpload.FileName));
            }

            UploadPanel.Visible = false;
            ResizePanel.Visible = true;
        }
        // Protected Methods (1) 

        protected void Page_Load(object sender, EventArgs e)
        {
            string file        = Request.Params["fileid"];
            string fileName    = Request.Params["filename"];
            string delete      = Request.Params["delete"];
            string contentType = Request.Params["ct"];

            if (string.IsNullOrEmpty(file))
            {
                throw new HttpException(404, "HTTP/1.1 404 Not Found");
            }
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = file;
            }

            try
            {
                byte[] bytes;

                using (var epmLiveFileStore = new EPMLiveFileStore(SPContext.Current.Web))
                {
                    bytes = epmLiveFileStore.Get(file);

                    if (!string.IsNullOrEmpty(delete) && delete.Equals("1"))
                    {
                        epmLiveFileStore.Delete(file);
                    }
                }

                if (!string.IsNullOrEmpty(contentType))
                {
                    Response.ContentType = contentType;
                }

                Response.AddHeader("Content-Disposition", @"attachment;filename=""" + fileName + @"""");
                Response.Buffer = true;
                EnableViewState = false;

                Response.BinaryWrite(bytes);
                Response.End();
            }
            catch (Exception)
            {
                throw new HttpException(404, "HTTP/1.1 404 Not Found");
            }
        }
        protected void ImportMain(bool cancelExistingJob)
        {
            if (cancelExistingJob)
            {
                // Again calling this method to fetch currently running job id to cancel it.
                IsImportResourceAlreadyRunning();
                Timer.CancelTimerJob(SPContext.Current.Web, jobUid);
            }
            if (!FileUpload.HasFile)
            {
                return;
            }

            string extension = Path.GetExtension(FileUpload.FileName);

            if (string.IsNullOrEmpty(extension))
            {
                return;
            }

            if (!extension.ToLower().Equals(".xlsm"))
            {
                CustomValidator.ErrorMessage = "Files with extension " + extension + " are not allowed.";
                CustomValidator.IsValid      = false;
            }
            else
            {
                string fileId;

                SPWeb spWeb = SPContext.Current.Web;

                using (var epmLiveFileStore = new EPMLiveFileStore(spWeb))
                {
                    fileId = epmLiveFileStore.Add(FileUpload.FileBytes);
                }

                SPSite spSite = spWeb.Site;

                Guid jobId = Timer.AddTimerJob(spSite.ID, spWeb.ID, "Import Resources", 60, fileId, string.Empty, -1, 9,
                                               string.Empty);
                Timer.Enqueue(jobId, 0, spSite);

                Response.Redirect(spWeb.Url + "/_layouts/epmlive/importresourcestatus.aspx?isdlg=1&jobid=" + jobId);
            }
        }
        // Protected Methods (3) 

        protected void OnSaveButtonClicked(object sender, EventArgs e)
        {
            SPUtility.ValidateFormDigest();

            byte[] pic;

            string resizeInfo = ResizeInfoField.Value;

            if (!string.IsNullOrEmpty(resizeInfo))
            {
                pic = ResizeImage(resizeInfo);
            }
            else
            {
                using (var fileStore = new EPMLiveFileStore(Web))
                {
                    pic = fileStore.Get(FileNameField.Value);
                }
            }

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (var spSite = new SPSite(Web.Site.ID))
                {
                    using (SPWeb spWeb = spSite.OpenWeb(Web.ID))
                    {
                        using (var fileStore = new EPMLiveFileStore(spWeb))
                        {
                            fileStore.Delete(FileNameField.Value);
                        }
                    }
                }
            });

            SavePicture(pic);
        }