示例#1
0
        protected void gvFileAttachment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
            case "Attach":
            {
                if (gvFileAttachment.FooterRow != null)
                {
                    FileUpload fu     = (FileUpload)gvFileAttachment.FooterRow.FindControl("fileUpload");
                    Literal    litMsg = (Literal)gvFileAttachment.FooterRow.FindControl("addAttachmentMsg");


                    if (fu.HasFile)
                    {
                        FileInfo fInfo = new FileInfo(fu.PostedFile.FileName);

                        if (!FileUploadHelper.IsFileForbidden(fInfo.Extension))
                        {
                            int fileSize    = (fu.PostedFile.ContentLength + 512) / 1024;
                            int maxFileSize = int.Parse(ConfigurationManager.AppSettings["MaxFileSize"].ToString());

                            if (fileSize < maxFileSize)
                            {
                                string original = fu.FileName.ToString().Trim();
                                string actual   = FileUploadHelper.GetNewAlternativeFileName(Constant.FILEATTACHMENTSFOLDERDIR + Session[Constant.SESSION_USERID.ToString()].ToString() + "\\" + Session[Constant.SESSION_BIDREFNO.ToString()].ToString() + "\\", Session[Constant.SESSION_USERID.ToString()].ToString() + "_" + Session[Constant.SESSION_BIDREFNO.ToString()].ToString() + "_", fInfo.Extension);
                                try
                                {
                                    if (!Directory.Exists((Constant.FILEATTACHMENTSFOLDERDIR) + "\\" + Session[Constant.SESSION_USERID.ToString()].ToString() + "\\" + Session[Constant.SESSION_BIDREFNO.ToString()]))
                                    {
                                        Directory.CreateDirectory((Constant.FILEATTACHMENTSFOLDERDIR) + "\\" + Session[Constant.SESSION_USERID.ToString()].ToString() + "\\" + Session[Constant.SESSION_BIDREFNO.ToString()]);
                                        fu.SaveAs((Constant.FILEATTACHMENTSFOLDERDIR) + "\\" + Session[Constant.SESSION_USERID.ToString()].ToString() + "\\" + Session[Constant.SESSION_BIDREFNO.ToString()].ToString() + "\\" + actual);
                                        Attach(original, actual);
                                    }
                                    else
                                    {
                                        fu.SaveAs((Constant.FILEATTACHMENTSFOLDERDIR) + "\\" + Session[Constant.SESSION_USERID.ToString()].ToString() + "\\" + Session[Constant.SESSION_BIDREFNO.ToString()].ToString() + "\\" + actual);
                                        Attach(original, actual);
                                    }
                                }
                                catch
                                {
                                    litMsg.Text = BR + "&nbsp;&nbsp;&nbsp;" + BULLET + " File cannot be uploaded.";
                                }
                            }
                            else
                            {
                                litMsg.Text = BR + "&nbsp;&nbsp;&nbsp;" + BULLET + " File size exceeds limit(" + maxFileSize + "KB).";
                            }
                        }
                        else
                        {
                            litMsg.Text = BR + "&nbsp;&nbsp;&nbsp;" + BULLET + " File cannot be uploaded. The file type is forbidden to be uploaded.";
                        }
                    }
                }
            } break;

            case "Remove":
            {
                Remove(int.Parse(e.CommandArgument.ToString()));
            } break;

            case "Download":
            {
                string[] args = e.CommandArgument.ToString().Split(new char[] { '|' });
                string   path = Constant.FILEATTACHMENTSFOLDERDIR + Session[Constant.SESSION_USERID.ToString()].ToString() + "\\" + Session[Constant.SESSION_BIDREFNO.ToString()].ToString() + "\\";
                FileHelper.DownloadFile(this.Page, path, args[0], args[1]);
            } break;
            }
        }