Пример #1
0
        public void Post([FromBody] Models.Presentation.AttachmentUploadModel documentModel)
        {
            string virtualPath = "/content/attachments/" + documentModel.incidentId.ToString() + "/";
            string filePath    = System.Web.HttpContext.Current.Server.MapPath(virtualPath);

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            string mimeType = documentModel.attachFileType.Split(';')[0].Substring(5);
            string fileExt  = string.Empty;

            switch (mimeType)
            {
            case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
                fileExt = ".docx";
                break;

            case "application/msword":
                fileExt = ".doc";
                break;

            case "application/pdf":
                fileExt = ".pdf";
                break;

            default:
                fileExt = ".txt";
                break;
            }

            string docFileName     = Guid.NewGuid().ToString() + fileExt;
            string docFileNamePath = filePath + "/" + docFileName;


            System.IO.File.WriteAllBytes(docFileNamePath, Convert.FromBase64String(documentModel.attachFileBase64));


            Models.IncidentAttachment attachment = new Models.IncidentAttachment();

            attachment.incidentId         = documentModel.incidentId;
            attachment.attachmentTypeId   = 2;
            attachment.attachmentUrl      = virtualPath + docFileName;
            attachment.attachmentTitle    = documentModel.attachmentTitle;
            attachment.attachmentComments = documentModel.attachmentComments;
            attachment.uploadedBy         = documentModel.userId;
            attachment.uploadedStamp      = DateTime.Now;
            attachment.isVoided           = false;

            this._db.IncidentAttachments.Add(attachment);
            this._db.SaveChanges();
        }
Пример #2
0
        public string Post([FromBody] Models.Presentation.AttachmentUploadModel photoModel)
        {
            const int maxWidth  = 800;
            const int maxHeight = 800;

            string virtualPath = "/content/attachments/" + photoModel.incidentId.ToString() + "/";
            string filePath    = System.Web.HttpContext.Current.Server.MapPath(virtualPath);

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            string photoFileName     = Guid.NewGuid().ToString() + ".png";
            string photoFileNamePath = filePath + "/" + photoFileName;

            byte[] imageBytes = Convert.FromBase64String(photoModel.attachFileBase64);

            MemoryStream stream = new MemoryStream(imageBytes, 0, imageBytes.Length);

            stream.Write(imageBytes, 0, imageBytes.Length);


            Image currentImage = Image.FromStream(stream, true);



            int scaledWidth  = 0;
            int scaledHeight = 0;



            scaledHeight = currentImage.Height;
            scaledWidth  = currentImage.Width;

            if (scaledWidth > maxWidth || scaledHeight > maxHeight)
            {
                int deltaWidth  = scaledWidth - maxWidth;
                int deltaHeight = scaledHeight - maxHeight;

                double scaleFactor = 1;

                if (deltaHeight > deltaWidth)
                {
                    scaleFactor = ((double)maxHeight) / ((double)scaledHeight);
                }
                else
                {
                    scaleFactor = ((double)maxWidth) / ((double)scaledWidth);
                }

                scaledHeight = Convert.ToInt16(scaleFactor * scaledHeight);
                scaledWidth  = Convert.ToInt16(scaleFactor * scaledWidth);
                System.Drawing.Size scaledSize = new System.Drawing.Size(scaledWidth, scaledHeight);

                System.Drawing.Bitmap scaledPhoto = new System.Drawing.Bitmap(currentImage, scaledSize);
                //scaledPhoto.Save(filePath + photoModel.photoFileName, codecInfo, encoderParameters);
                scaledPhoto.Save(photoFileNamePath, System.Drawing.Imaging.ImageFormat.Png);
                scaledPhoto.Dispose();
            }
            else
            {
                //currentImage.Save(filePath + photoModel.photoFileName, codecInfo, encoderParameters);
                currentImage.Save(photoFileNamePath, System.Drawing.Imaging.ImageFormat.Png);
            }



            currentImage.Dispose();

            stream.Close();
            stream.Dispose();



            Models.IncidentAttachment attachment = new Models.IncidentAttachment();

            attachment.incidentId         = photoModel.incidentId;
            attachment.attachmentTypeId   = 1;
            attachment.attachmentUrl      = virtualPath + photoFileName;
            attachment.attachmentTitle    = photoModel.attachmentTitle;
            attachment.attachmentComments = photoModel.attachmentComments;
            attachment.uploadedBy         = photoModel.userId;
            attachment.uploadedStamp      = DateTime.Now;
            attachment.isVoided           = false;

            this._db.IncidentAttachments.Add(attachment);
            this._db.SaveChanges();


            return(photoFileNamePath);
        }