Esempio n. 1
0
    protected void btnSubmit_Click(Object sender, EventArgs e)
    {
        bool   exists                = false;
        int    newAttachmentID       = 0;
        int    newModuleAttachmentID = 0;
        bool   saved    = false;
        string errorMsg = string.Empty;

        var dtfilesAttr = JsonConvert.DeserializeObject <DataTable>(inpHide.Value);

        if (this.Edit || (this.fileUpload1.HasFile))
        {
            if (!this.Edit)                  //adding an attachment, not editing one
            {
                int i = 0;
                foreach (HttpPostedFile uploadedFile in fileUpload1.PostedFiles)
                {
                    string   fileName       = "";
                    string   fileExtension  = "";
                    string   strFileTypeID  = "";
                    string   strDescription = "";
                    int      intAttachType  = 0;
                    Stream   fStream        = uploadedFile.InputStream;
                    int      fileLength     = uploadedFile.ContentLength;
                    byte[]   fByte          = new byte[fileLength];
                    string   fileType       = uploadedFile.ContentType;
                    string[] splitFileName  = fileName.Split('\\');
                    fileName = splitFileName[splitFileName.Length - 1];

                    fStream.Read(fByte, 0, fByte.Length);
                    fStream.Close();

                    if (fileUpload1.PostedFiles.Count > 1)
                    {
                        fileName      = dtfilesAttr.Rows[i]["name"].ToString();
                        strFileTypeID = dtfilesAttr.Rows[i]["type"].ToString();
                        int.TryParse(strFileTypeID, out intAttachType);
                        strDescription = dtfilesAttr.Rows[i]["description"].ToString();
                    }
                    else
                    {
                        fileName      = uploadedFile.FileName;
                        strFileTypeID = ddlAttachmentType.SelectedValue;
                        int.TryParse(ddlAttachmentType.SelectedValue, out intAttachType);
                        strDescription = txtDescription.Text;
                    }

                    splitFileName = fileName.Split('\\');
                    fileName      = splitFileName[splitFileName.Length - 1];
                    splitFileName = fileName.Split('.');
                    fileExtension = splitFileName[splitFileName.Length - 1];

                    switch (Module.ToUpper())
                    {
                    case "TASK":
                        saved = WorkItem_Task.Attachment_Add(WorkItemTaskID: this.workItemTaskID
                                                             , attachmentTypeID: intAttachType
                                                             , fileName: fileName
                                                             , title: strDescription
                                                             , description: strDescription
                                                             , fileData: fByte
                                                             , extensionID: 0
                                                             , newAttachmentID: out newAttachmentID
                                                             , newWorkItemAttachmentID: out newModuleAttachmentID
                                                             , errorMsg: out errorMsg);
                        break;

                    case "WORKITEM":
                        saved = WorkloadItem.Attachment_Add(workItemID: this.WorkItemID
                                                            , attachmentTypeID: intAttachType
                                                            , fileName: fileName
                                                            , title: strDescription
                                                            , description: strDescription
                                                            , fileData: fByte
                                                            , extensionID: 0
                                                            , newAttachmentID: out newAttachmentID
                                                            , newWorkItemAttachmentID: out newModuleAttachmentID
                                                            , errorMsg: out errorMsg);
                        break;

                    case "WORKREQUEST":
                        saved = WorkRequest.Attachment_Add(workRequestID: this.WorkRequestID
                                                           , attachmentTypeID: intAttachType
                                                           , fileName: fileName
                                                           , title: strDescription
                                                           , description: strDescription
                                                           , fileData: fByte
                                                           , extensionID: 0
                                                           , newAttachmentID: out newAttachmentID
                                                           , newWorkItemAttachmentID: out newModuleAttachmentID
                                                           , errorMsg: out errorMsg);
                        break;

                    case "MEETING":
                        saved = AOR.AORMeetingInstanceAttachment_Save(0, AORMeetingInstanceID, 0, intAttachType, fileName, strDescription, strDescription, fByte, out newAttachmentID);
                        break;

                    case "RQMTDESC":
                        saved = RQMT.RQMTDescriptionAttachment_Save(0, RQMTDescriptionID, 0, intAttachType, fileName, strDescription, fByte, out newAttachmentID);
                        break;

                    case "MDIMAGE":
                        Dictionary <string, string> result = new Dictionary <string, string> {
                            { "saved", "false" }, { "exists", "false" }, { "newID", "0" }, { "error", "" }
                        };
                        string[] allowedExtensions = new string[] { ".jpg", ".jpeg", ".png", ".gif" };
                        string   extension         = Path.GetExtension(fileName).ToLower();

                        if (!allowedExtensions.Contains(extension))
                        {
                            continue;
                        }

                        result   = MasterData.Image_Add(ImageName: strDescription, Description: strDescription, FileName: fileName, FileData: fByte);
                        saved    = result["saved"].ToUpper() == "TRUE";
                        errorMsg = result["error"];
                        break;

                    case "NEWS":
                        saved = WTSNews.NewsAttachment_Add(newsID: this.NewsID
                                                           , attachmentTypeID: (int)AttachmentTypeEnum.News
                                                           , fileName: fileName
                                                           , title: strDescription
                                                           , description: strDescription
                                                           , fileData: fByte
                                                           , extensionID: 0
                                                           , newAttachmentID: out newAttachmentID
                                                           //, newWorkItemAttachmentID: out newModuleAttachmentID
                                                           , errorMsg: out errorMsg);
                        break;

                    default:
                        saved = WTSData.Attachment_Add(attachmentTypeID: intAttachType
                                                       , fileName: fileName
                                                       , title: strDescription
                                                       , description: strDescription
                                                       , fileData: fByte
                                                       , extensionID: 0
                                                       , newID: out newAttachmentID
                                                       , errorMsg: out errorMsg);
                        break;
                    }

                    i += 1;
                }
            }
            else
            {
                string fileName = "";
                fileName = fileUpload1.FileName;

                //Stream fStream = fileUpload1.PostedFile.InputStream;
                //int fileLength = fileUpload1.PostedFile.ContentLength;

                //byte[] fByte = new byte[fileLength];

                string fileType = string.Empty;
                if (fileUpload1.PostedFile != null)
                {
                    fileName = fileUpload1.PostedFile.FileName;
                    fileType = fileUpload1.PostedFile.ContentType;
                }
                string[] splitFileName = fileName.Split('\\');
                fileName      = splitFileName[splitFileName.Length - 1];
                splitFileName = fileName.Split('.');

                string fileExtension = splitFileName[splitFileName.Length - 1];
                //fStream.Read(fByte, 0, fByte.Length);
                //fStream.Close();

                if (fileName == "")
                {
                    fileName = this.OriginalFileName;
                }

                string attachmentType = ddlAttachmentType.SelectedValue;
                if (Module == "News")
                {
                    attachmentType = ((int)AttachmentTypeEnum.News).ToString();
                }

                saved = WTSData.Attachment_Update(attachmentID: this.AttachmentID
                                                  , attachmentTypeID: int.Parse(attachmentType)
                                                  , fileName: fileName
                                                  , title: txtDescription.Text
                                                  , description: txtDescription.Text
                                                  //, fileData: fByte
                                                  , exists: out exists
                                                  , errorMsg: out errorMsg);
            }

            if (!saved || errorMsg.Length > 0)
            {
                lblError.Text = errorMsg;
            }
            else
            {
                if (!exists)
                {
                    HasUploaded = 1;
                }
                else
                {
                    AlreadyExists = 1;
                }

                if (Module.ToUpper() == "WORKITEM")
                {
                    Workload.SendWorkloadEmail("WorkItem", false, this.WorkItemID);
                }
            }
        }
    }