Esempio n. 1
0
    public static bool RQMTDescriptionAttachment_Save(int RQMTDescriptionAttachmentID, int RQMTDescription_ID, int attachmentID, int attachmentTypeId, string fileName, string description, byte[] fileData, out int newAttachmentID)
    {
        newAttachmentID = 0;

        try
        {
            int newID = 0;

            string errors = null;
            WTSData.Attachment_Add(attachmentTypeId, fileName, "RQMTDESC ATT", description, fileData, 0, out newID, out errors);

            if (newID > 0)
            {
                using (SqlConnection cn = new SqlConnection(WTSCommon.WTS_ConnectionString))
                {
                    cn.Open();

                    using (SqlCommand cmd = new SqlCommand("RQMTDescriptionAttachment_Save", cn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.Add("@RQMTDescriptionAttachmentID", SqlDbType.Int).Value = 0;
                        cmd.Parameters.Add("@RQMTDescriptionID", SqlDbType.Int).Value           = RQMTDescription_ID;
                        cmd.Parameters.Add("@AttachmentID", SqlDbType.Int).Value = newID;

                        cmd.ExecuteNonQuery();

                        newAttachmentID = newID;
                    }
                }
            }
            else
            {
                LogUtility.LogError(errors);
                return(false);
            }
        }
        catch (Exception ex)
        {
            LogUtility.LogException(ex);
            return(false);
        }

        return(true);
    }
Esempio n. 2
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);
                }
            }
        }
    }
Esempio n. 3
0
    public static bool NewsAttachment_Add(int newsID
                                          , int attachmentTypeID
                                          , string fileName
                                          , string title
                                          , string description
                                          , byte[] fileData
                                          , int extensionID
                                          , out int newAttachmentID
                                          , out string errorMsg)
    {
        bool saved = false, exists = false;
        int  newID = 0;

        newAttachmentID = 0;

        saved = WTSData.Attachment_Add(attachmentTypeID: attachmentTypeID, fileName: fileName, title: title, description: description, fileData: fileData, extensionID: extensionID, newID: out newAttachmentID, errorMsg: out errorMsg);
        if (saved && newAttachmentID > 0)
        {
            string procName = "NewsAttachment_Add";

            using (SqlConnection cn = new SqlConnection(WTSCommon.WTS_ConnectionString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(procName, cn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@NewsID", SqlDbType.Int).Value         = newsID;
                    cmd.Parameters.Add("@AttachmentID", SqlDbType.Int).Value   = newAttachmentID;
                    cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = HttpContext.Current.User.Identity.Name;
                    cmd.Parameters.Add("@newID", SqlDbType.Int).Direction      = ParameterDirection.Output;

                    string mystr = string.Join(";/nnn/ ", cmd.Parameters.Cast <System.Data.SqlClient.SqlParameter>()
                                               .Where(p => p.SqlDbType.ToString() == "NVarChar")
                                               .Where(p => p.Value != null)
                                               .Select(p => "declare " + p.ParameterName + " " + p.SqlDbType + "(MAX) = '" + p.Value.ToString() + "'"));

                    mystr += "/nnn/ " + string.Join(";/nnn/ ", cmd.Parameters.Cast <System.Data.SqlClient.SqlParameter>()
                                                    .Where(p => p.SqlDbType.ToString() == "Int")
                                                    .Where(p => p.Value != null)
                                                    .Select(p => "declare " + p.ParameterName + " " + p.SqlDbType + " = " + p.Value.ToString() + ""));

                    mystr += "/nnn/ " + string.Join(";/nnn/ ", cmd.Parameters.Cast <System.Data.SqlClient.SqlParameter>()
                                                    .Where(p => p.SqlDbType.ToString() == "bit")
                                                    .Where(p => p.Value != null)
                                                    .Select(p => "declare " + p.ParameterName + " " + p.SqlDbType + " = " + p.Value.ToString() + ""));

                    mystr += "/nnn/ " + string.Join(";/nnn/ ", cmd.Parameters.Cast <System.Data.SqlClient.SqlParameter>()
                                                    .Where(p => p.DbType.ToString() != "Xml")
                                                    .Where(p => p.Value == null)
                                                    .Select(p => "declare " + p.ParameterName + " " + p.SqlDbType + " = null"));

                    cmd.ExecuteNonQuery();

                    SqlParameter paramNewID = cmd.Parameters["@newID"];
                    if (paramNewID != null)
                    {
                        saved = true;
                    }
                }
            }
        }

        return(saved);
    }