Exemplo n.º 1
0
        /// <summary>
        /// 上传文件d
        /// </summary>
        /// <param name="file"></param>
        private string UploadFile(HttpPostedFileBase file, string attachmentId)
        {
            // 添加这个附件
            byte[] contents = new byte[file.ContentLength];
            file.InputStream.Read(contents, 0, file.ContentLength);
            OThinker.H3.Data.Attachment attachment = new OThinker.H3.Data.Attachment();
            attachment.ObjectID            = attachmentId;
            attachment.Content             = contents;
            attachment.ContentType         = file.ContentType;
            attachment.CreatedBy           = this.UserValidator.UserID;
            attachment.CreatedTime         = System.DateTime.Now;
            attachment.FileName            = System.IO.Path.GetFileName(ReplaceFileName(file.FileName));
            attachment.LastVersion         = true;
            attachment.ModifiedBy          = null;
            attachment.ModifiedTime        = System.DateTime.Now;
            attachment.BizObjectSchemaCode = this.SchemaCode;
            attachment.ContentLength       = file.ContentLength;
            attachment.BizObjectId         = this.BizObjectID;
            attachment.DataField           = this.DataField;
            // 禁止下载
            //if (this.chkDisableDownload.Checked)
            //{
            //    attachment.FileFlag |= Data.Attachment.FileFlag_DisableDownload;
            //}
            //// 禁止打印
            //if (this.chkDisablePrint.Checked)
            //{
            //    attachment.FileFlag |= Data.Attachment.FileFlag_DisablePrint;
            //}

            return(this.Engine.BizObjectManager.AddAttachment(attachment));
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        string dataField, attachmentId;

        dataField    = HttpUtility.UrlDecode(Request.QueryString["dataField"]);
        attachmentId = Request.QueryString["AttachmentID"];
        string bizObjectID = Request.QueryString["BizObjectID"] + string.Empty;
        string instanceID  = Request.QueryString["InstanceID"] + string.Empty;
        string schemaCode  = HttpUtility.UrlDecode(Request.QueryString["SchemaCode"] + string.Empty);

        if (string.IsNullOrEmpty(attachmentId))
        {
            //先查找BizObjectID为InstanceID的附件,然后在找BizObjectID的附件
            OThinker.H3.Data.AttachmentHeader[] attachs = AppUtility.Engine.BizObjectManager.QueryAttachment(schemaCode, instanceID, dataField, OThinker.Data.BoolMatchValue.True, null);
            if (attachs != null && attachs.Length > 0)
            {
                System.Collections.Generic.List <string> lstAttachmentids = new System.Collections.Generic.List <string>();
                foreach (OThinker.H3.Data.AttachmentHeader header in attachs)
                {
                    lstAttachmentids.Add(header.AttachmentID);
                }

                //更新BizObjectID
                AppUtility.Engine.BizObjectManager.AttachBizObject(
                    OThinker.Organization.User.AdministratorID,
                    lstAttachmentids.ToArray(),
                    schemaCode,
                    bizObjectID,
                    dataField);
            }

            OThinker.H3.Data.AttachmentHeader[] attachments = AppUtility.Engine.BizObjectManager.QueryAttachment(schemaCode, bizObjectID, dataField, OThinker.Data.BoolMatchValue.True, null);
            if (attachments != null && attachments.Length > 0)
            {
                attachmentId = attachments[0].ObjectID;
            }
        }

        if (!string.IsNullOrEmpty(attachmentId))
        {
            OThinker.H3.Data.Attachment attachment = AppUtility.Engine.BizObjectManager.GetAttachment(string.Empty,
                                                                                                      schemaCode,
                                                                                                      bizObjectID,
                                                                                                      attachmentId);

            //设定输出文件类型
            Response.ContentType = attachment.ContentType;
            // 输出文件内容
            Response.OutputStream.Write(attachment.Content, 0, attachment.Content.Length);
            Response.End();
        }
        else
        {
            // Response.Write("<script>alert('" + this.PortalResource.GetString("SheetOffcie_HasPDF") + "');</script>");
        }
    }
        /// <summary>
        /// 保存NTKO附件
        /// </summary>
        /// <returns></returns>
        public JsonResult SaveOfficeAttachment()
        {
            return(ExecuteFunctionRun(() =>
            {
                ActionResult result = new ActionResult(true, "");
                if (Request.Files.Count == 0)
                {
                    Response.End();
                    result.Success = false;
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                string ID = Request.Form["ID"];
                string dataField = Request.Form["dataField"];
                string instanceID = Request.Form["InstanceID"] + string.Empty;
                string download = Request.Form["download"] + string.Empty;
                string bizObjectID = Request.Form["BizObjectID"] + string.Empty;
                string schemaCode = Request.Form["SchemaCode"] + string.Empty;
                //string workflowPackage = Request.Form["WorkflowPackage"] + string.Empty;
                //string workflowName = Request.Form["WorkflowName"] + string.Empty;

                byte[] fileValue = new byte[Request.Files[0].ContentLength];
                if (fileValue.Length == 0)
                {
                    return null;                        // 没有接收到数据,则不做保存
                }
                Request.Files[0].InputStream.Read(fileValue, 0, Request.Files[0].ContentLength);
                OThinker.H3.Data.Attachment attachment = new OThinker.H3.Data.Attachment()
                {
                    ObjectID = ID,
                    FileName = Server.UrlDecode(Request.Files[0].FileName),
                    ContentType = Request.Files[0].ContentType,
                    Content = fileValue,
                    DataField = Server.UrlDecode(dataField),
                    CreatedBy = this.UserValidator.UserID,
                    Downloadable = true,
                    BizObjectId = bizObjectID,
                    BizObjectSchemaCode = schemaCode
                };
                try
                {
                    this.Engine.BizObjectManager.RemoveAttachment(string.Empty,
                                                                  schemaCode,
                                                                  attachment.BizObjectId,
                                                                  attachment.AttachmentID);
                    this.Engine.BizObjectManager.AddAttachment(attachment);
                }
                catch (Exception ex)
                {
                    this.Engine.LogWriter.Write("WordSave Error:" + ex.ToString());
                }

                return Json(result, JsonRequestBehavior.AllowGet);
            }));
        }
        public JsonResult OpenOfficeAttachment()
        {
            return(ExecuteFunctionRun(() =>
            {
                ActionResult result = new ActionResult(true, "");
                string dataField, attachmentId;
                dataField = OThinker.Data.Convertor.SqlInjectionPrev(Server.UrlDecode(Request.QueryString["dataField"]));
                attachmentId = OThinker.Data.Convertor.SqlInjectionPrev(Request.QueryString["AttachmentID"]);
                string bizObjectID = OThinker.Data.Convertor.SqlInjectionPrev(Request.QueryString["BizObjectID"] + string.Empty);
                string schemaCode = OThinker.Data.Convertor.SqlInjectionPrev(Server.UrlDecode(Request.QueryString["SchemaCode"] + string.Empty));
                if (string.IsNullOrEmpty(attachmentId))
                {
                    OThinker.H3.Data.AttachmentHeader[] attachments = this.Engine.BizObjectManager.QueryAttachment(schemaCode, bizObjectID, dataField, OThinker.Data.BoolMatchValue.True, null);
                    if (attachments != null && attachments.Length > 0)
                    {
                        attachmentId = attachments[0].ObjectID;
                    }
                }

                if (!string.IsNullOrEmpty(attachmentId))
                {
                    OThinker.H3.Data.Attachment attachment = this.Engine.BizObjectManager.GetAttachment(string.Empty,
                                                                                                        schemaCode,
                                                                                                        bizObjectID,
                                                                                                        attachmentId);

                    //设定输出文件类型
                    Response.ContentType = attachment.ContentType;
                    // 输出文件内容
                    Response.OutputStream.Write(attachment.Content, 0, attachment.Content.Length);
                    Response.End();
                }
                //else
                //    Response.Write("<script>alert('" + this.PortalResource.GetString("SheetOffcie_HasPDF") + "');</script>");

                return Json(result, JsonRequestBehavior.AllowGet);
            }));
        }
        /// <summary>
        /// 上传文件到微信并获取MediaID
        /// </summary>
        /// <param name="Attachment"></param>
        /// <returns>上传微信后得到的MediaID</returns>
        string UploadFile(OThinker.H3.Data.Attachment Attachment)
        {
            NameValueCollection nvc = new NameValueCollection();
            string boundary         = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(string.Format(URL_UploadFile, AccessToken, "file"));

            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method      = "POST";
            wr.KeepAlive   = true;
            wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

            Stream rs = wr.GetRequestStream();

            string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

            foreach (string key in nvc.Keys)
            {
                rs.Write(boundarybytes, 0, boundarybytes.Length);
                string formitem      = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                rs.Write(formitembytes, 0, formitembytes.Length);
            }
            rs.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\nContent-Type: {1}\r\n\r\n";
            string header         = string.Format(headerTemplate, Attachment.FileName, Attachment.ContentType);

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);

            rs.Write(Attachment.Content, 0, Attachment.Content.Length);

            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();

            WebResponse wresp = null;

            try
            {
                wresp = wr.GetResponse();
                Stream       stream2 = wresp.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);

                byte[] reByte = new Byte[wresp.ContentLength];

                Stream s = wresp.GetResponseStream();
                s.Read(reByte, 0, (int)wresp.ContentLength);

                string ret = System.Text.Encoding.UTF8.GetString(reByte, 0, reByte.Length).ToString();
                Dictionary <string, string> JSON =
                    (Dictionary <string, string>)Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(ret);

                if (JSON != null && JSON.ContainsKey("media_id"))
                {
                    return(JSON["media_id"]);
                }
            }
            catch (Exception ex)
            {
                this.LogWriter.Write("上传文件到微信错误:" + ex.ToString());
                if (wresp != null)
                {
                    wresp.Close();
                    wresp = null;
                }
            }
            finally
            {
                wr = null;
            }
            return(null);
        }
        public JsonResult UpFileData()
        {
            return(ExecuteFunctionRun(() =>
            {
                ActionResult result = new ActionResult(true, "");
                string strNewAttachIds = string.Empty;
                string dataField = Request.Form["dataField"];
                string instanceID = Request.Form["InstanceID"] + string.Empty;
                //新加的属性,待确认
                //ERROR:
                string schemaCode = Request.Form["SchemaCode"] + string.Empty;
                string bizObjectID = Request.Form["BizObjectID"] + string.Empty;
                //string workflowPackage = Request.Form["WorkflowPackage"] + string.Empty;
                //string workflowName = Request.Form["WorkflowName"] + string.Empty;
                if (dataField == string.Empty)
                {
                    result.Success = false;
                    result.Message = "数据项名称为空";
                    return Json(result);
                }
                System.Web.HttpPostedFileBase theFile;
                //说明:若控件的参数 <PARAM NAME="DelFileField" VALUE="DELETE_FILE_NAMES">
                //则,从Request.Form得到的DELETE_FILE_NAMES集合包含了所有需要删除的文件
                //也就是客户端修改过的文件名
                string[] delFileNames = Request.Form.GetValues("DELETE_FILE_NAMES");
                System.Web.HttpFileCollectionBase uploadFiles = Request.Files;

                try
                {
                    OThinker.H3.Data.AttachmentHeader[] headers = null;
                    if (instanceID != string.Empty)
                    {
                        headers = AppUtility.Engine.BizObjectManager.QueryAttachment(schemaCode, bizObjectID, dataField, OThinker.Data.BoolMatchValue.True, string.Empty);
                    }

                    // 增加保存附件文件
                    for (int i = 0; i < uploadFiles.Count; i++)
                    {
                        theFile = uploadFiles[i];
                        // "EDITFILE"和客户端的BeginSaveToURL的第二个参数一致
                        if (uploadFiles.GetKey(i).ToUpper() == "EDITFILE")
                        {
                            // 写入FILE数据
                            string fname = theFile.FileName;
                            int fsize = theFile.ContentLength;
                            string contentType = theFile.ContentType;
                            byte[] buffer = new byte[theFile.ContentLength];
                            theFile.InputStream.Read(buffer, 0, fsize);

                            OThinker.H3.Data.Attachment attachment = new OThinker.H3.Data.Attachment()
                            {
                                ObjectID = Guid.NewGuid().ToString(),
                                FileName = fname,
                                ContentType = string.Empty,
                                Content = buffer,
                                DataField = Server.UrlDecode(dataField),
                                CreatedBy = this.UserValidator.UserID,
                                Downloadable = true,
                                BizObjectId = bizObjectID,
                                BizObjectSchemaCode = schemaCode
                            };

                            strNewAttachIds += attachment.ObjectID + ",";

                            this.Engine.BizObjectManager.AddAttachment(attachment);
                        }
                    }
                    if (headers != null && delFileNames != null)
                    {
                        foreach (OThinker.H3.Data.AttachmentHeader header in headers)
                        {
                            if (Array.IndexOf <string>(delFileNames, header.FileName) > -1)
                            {
                                this.Engine.BizObjectManager.RemoveAttachment(this.UserValidator.UserID,
                                                                              schemaCode,
                                                                              header.BizObjectId,
                                                                              header.ObjectID);
                            }
                        }
                    }
                    // Response.Write(strNewAttachIds);
                    result.Message = strNewAttachIds;
                    return Json(result);
                }
                catch (Exception err)
                {
                    result.Success = false;
                    result.Message = "NTKO Attachment ERROR:" + err.ToString();
                    AppUtility.Engine.LogWriter.Write("NTKO Attachment ERROR:" + err.ToString());
                    return Json(result);
                }
            }));
        }