JAVA工具
          JAVA使用軟件 開(kāi)發(fā)工具
          posts - 87,  comments - 9,  trackbacks - 0

          using System;
          using System.Collections;
          using System.Configuration;
          using System.Data;
          using System.IO;
          using System.Linq;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.HtmlControls;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Xml.Linq;
          using Common;
          using Common.Assignment;
          using Common.Attachment;
          using Common.Log;
          using Common.TBL;
          using websiteCore.LINQ.ULT;
          using websiteCore.LINQ.ATC;
          using websiteCore.Common;

          namespace WebPartLib._layouts.website_Resources.ExtPages
          {
              public partial class UploadService : System.Web.UI.Page
              {
                  protected string strJson = "";

                  protected void Page_Load(object sender, EventArgs e)
                  {
                      string id = Request["Id"];
                      string action = Request["action"];
                      string knowledge_id = Request["lid"];

                      if (action != null)
                      {
                          if ("upload".Equals(action))
                          {
                              UploadAttachment(HttpContext.Current, knowledge_id);
                              //UoloadAttachment2(HttpContext.Current, knowledge_id);
                          }
                          else if ("DownLoad".Equals(action))
                          {
                              DownLoadAttachment(HttpContext.Current, id);
                          }
                      }
                  }

                  /// <summary>
                  /// 下載附件
                  /// </summary>
                  /// <param name="context"></param>
                  /// <param name="knowledge_id"></param>
                  private void DownLoadAttachment(HttpContext context, string attachmentId)
                  {
                      try
                      {
                          if (!string.IsNullOrEmpty(attachmentId))
                          {
                              using (var atcContext = ATCDBHelper.DataContext)
                              {
                                  //CommonUtil.AddAppErrorLog("CHG000001136", "BPS", "BEGIN_DOWNLOAD" + attachmentId, null);
                                  var obj = atcContext.Usp_ATC_DownLoadAttachmentContent(attachmentId).FirstOrDefault();
                                  //CommonUtil.AddAppErrorLog("CHG000001136", "BPS", "END_DOWNLOAD", null);
                                  string attachmentName = obj.AttachmentName;
                                  byte[] data = obj.AttachmentContent.ToArray();
                                 
                                  //獲得文件后綴
                                  int index = attachmentName.LastIndexOf(".");
                                  string suffixal = attachmentName.Substring((index + 1), attachmentName.Length - (index + 1));

                                  string contextType = "";
                                  switch (suffixal)
                                  {
                                      case "xls":
                                      case "xlsx":
                                          contextType = "application/vnd.ms-excel";
                                          break;
                                      case "jpeg":
                                      case "jpg":
                                      case "jpe":
                                      case "png":
                                      case "tif":
                                      case "tiff":
                                      case "gif":
                                          contextType = "image/jpeg";
                                          break;
                                      case "docx":
                                      case "doc":
                                          contextType = "application/msword";
                                          break;
                                      case "pdf":
                                          contextType = "application/pdf";
                                          break;
                                      case "ppt":
                                          contextType = "application/vnd.ms-powerpoint";
                                          break;
                                      case "js":
                                          contextType = "application/x-javascript";
                                          break;
                                      case "zip":
                                          contextType = "application/zip";
                                          break;
                                      default:
                                          contextType = "application/octet-stream";
                                          break;
                                  }
                                  context.Response.ContentType = contextType;

                                  context.Response.Charset = "GB2312";
                                  context.Response.ContentEncoding = System.Text.Encoding.UTF8;

                                  //string srcName = System.Web.HttpUtility.UrlEncode(attachmentName);
                                  //string tagName = System.Web.HttpUtility.UrlDecode(srcName, System.Text.Encoding.UTF8);

                                  //System.Text.Encoding uft8 = System.Text.Encoding.UTF8;// GetEncoding(65001);
                                  ////System.Text.Encoding gb2312 = System.Text.Encoding.GetEncoding("GB2312");
                                  //byte[] strTemp1 = System.Text.Encoding.Default.GetBytes(attachmentName); ;
                                  //byte[] strTemp2 = System.Text.Encoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.UTF8, strTemp1);
                                  //string FileName = uft8.GetString(strTemp2);


                                  //if (strTemp.Length > 150)
                                  //{
                                  //}
                                  //System.Text.Encoding iso8859, gb2312;
                                  //iso8859 = System.Text.Encoding.GetEncoding("ISO8859-1");
                                  //gb2312 = System.Text.Encoding.UTF8;
                                  //byte[] gb2;
                                  //gb2 = gb2312.GetBytes(attachmentName);
                                  //string iso = iso8859.GetString(gb2);

                                  Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(attachmentName));
                                  //Response.AddHeader("Content-Disposition", "attachment;filename=" + tagName);
                                  Response.AddHeader("Content-Length", data.Length.ToString());
                                  if (data.Length > 0)
                                  {
                                      Response.BinaryWrite(data);
                                  }
                                  context.ApplicationInstance.CompleteRequest();
                              }
                          }
                      }
                      catch(Exception err)
                      {
                          CommonUtil.AddAppErrorLog("CHG000001136", "BPS", "DOWNLOAD_ERROR" + err.Message, null);
                          Response.StatusCode = 500;
                      }
                  }

                  //上傳附件控件2
                  private void UoloadAttachment2(HttpContext context, string knowledge_id)
                  {
                     JSONHelper jsonHelper = new JSONHelper();

                      string account = CommonMethods.IdentityName();
                      string strFileName = Path.GetFileName(context.Request.Files[0].FileName);
                      string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                      string strFileNameNoExt = Path.GetFileNameWithoutExtension(context.Request.Files[0].FileName);

                      Stream stream = context.Request.Files[0].InputStream;
                      var fileLength = Math.Round((double)stream.Length / 1024, 2);
                      //文件過(guò)大禁止上傳
                      if (fileLength > 10240)
                      {
                          jsonHelper.AddItem("status_error", "上傳文件不能超過(guò)10M.");
                          jsonHelper.ItemOk();
                      }
                      else
                      {
                          //保存
                         string dirPath = Server.MapPath("~/website_Upload");
                         string tmp=DateTime.Now.ToString("yyyyMMdd");
                         string filetype=knowledge_id.Substring(0, 3);

                         dirPath += "\\" + filetype + "\\" + tmp.Substring(0, 4) + "\\" + tmp.Substring(4, 2);

                         string filePath = "website_Upload/" + filetype + "/" + tmp.Substring(0, 4) + "/" + tmp.Substring(4, 2);

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

                         dirPath += "\\" + strFileName;
                         filePath += "/" + strFileName;

                         context.Request.Files[0].SaveAs(dirPath);
                          jsonHelper.AddItem("status_error", "OK");
                          jsonHelper.AddItem("url", filePath);
                          jsonHelper.AddItem("name", strFileName);
                          jsonHelper.AddItem("ext", strExtension);
                          jsonHelper.AddItem("filesize", fileLength.ToString());
                          jsonHelper.AddItem("uploader", CommonMethods.GetUserName(account));
                          jsonHelper.ItemOk();
                      }
                      strJson = jsonHelper.ToString();
                  }
                 
                  /// <summary>
                  /// 上傳附件控制
                  /// </summary>
                  /// <param name="context"></param>
                  /// <param name="knowledge_id"></param>
                  private void UploadAttachment(HttpContext context, string knowledge_id)
                  {
                      JSONHelper jsonHelper = new JSONHelper();

                      int attachmentType = 0;
                      int.TryParse(context.Request["attachmentType"], out attachmentType);
                      string account = CommonMethods.IdentityName();
                      string strFileName = Path.GetFileName(context.Request.Files[0].FileName);
                      string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                      string strFileNameNoExt = Path.GetFileNameWithoutExtension(context.Request.Files[0].FileName);

                      Stream stream = context.Request.Files[0].InputStream;
                      var fileLength = Math.Round((double)stream.Length / 1024, 2);
                      //文件過(guò)大禁止上傳
                      if (fileLength > 10240)
                      {
                          jsonHelper.AddItem("status_error", "上傳文件不能超過(guò)10M.");
                          jsonHelper.ItemOk();
                      }
                      else
                      {
                          //int currentVer = 0;
                          ////檢查文件名是否已經(jīng)存在,如果存在需要在后面加(1),(2)...
                          //using (var dataContext = ULTDBHelper.DataContext)
                          //{
                          //    var obj = (from r in dataContext.Tbl_UltAttachment
                          //               where r.AttachmentName.Trim().Equals(strFileName.Trim())
                          //               orderby r.FileLastV descending
                          //               select r.FileLastV).FirstOrDefault();

                          //    //已經(jīng)存在相同的文件名
                          //    if (obj != null)
                          //    {
                          //        currentVer = obj.Value;
                          //        //增加文件后綴數(shù)
                          //        currentVer = currentVer + 1;
                          //        strFileName = strFileNameNoExt + "(" + currentVer.ToString() + ")" + strExtension;
                          //    }
                          //}

                          //存入MOSS數(shù)據(jù)庫(kù)
                          //WssField[] fields = new WssField[] {
                          //    new WssField { fieldName = AttachmentService.FORMID, fieldValue = knowledge_id},
                          //    new WssField { fieldName = AttachmentService.NAME, fieldValue = strFileNameNoExt},//shortName},                   
                          //    new WssField { fieldName = AttachmentService.SIZE, fieldValue = fileLength},
                          //    new WssField { fieldName = AttachmentService.LABELID, fieldValue = "5"},
                          //    new WssField { fieldName = AttachmentService.CREATOR, fieldValue = account},
                          //    new WssField { fieldName = AttachmentService.CREATETIME, fieldValue = DateTime.Now},
                          //    new WssField { fieldName = AttachmentService.FLAG, fieldValue = " "}
                          //};
                          /******************* modify by xml *********************
                           * 從2011.11.15號(hào)起附件存入SQLServer 2005,不存入MOSS中
                           */
                          using (var ultContext = ULTDBHelper.DataContext)
                          {
                              try
                              {
                                  byte[] data = new byte[stream.Length];
                                  int length = data.Length;
                                  stream.Read(data, 0, length);
                                  stream.Close();

                                  //存入website_database下的tbl_UltAttachment,獲得附件編號(hào)attachmentId
                                  string attachmentId = string.Empty;
                                  ultContext.Usp_ULT_SaveAttachment(
                                              knowledge_id,
                                              attachmentType,
                                              strFileName,
                                              "SQLServer2005",
                                              account,
                                              (decimal)fileLength,
                                              0,
                                              0,
                                              ref attachmentId);
                                  //將內(nèi)容存入website_attachment_database
                                  using(var atcContext = ATCDBHelper.DataContext)
                                  {
                                      atcContext.Usp_ATC_UpdateAttachmentContent(attachmentId, data);
                                  }
                                  //Response.Write("{success: true}");
                                  jsonHelper.AddItem("status_error", "OK");
                                  jsonHelper.AddItem("Id", attachmentId);
                                  jsonHelper.AddItem("fileId", "KBA" + attachmentId);
                                  jsonHelper.AddItem("url", "_layouts/website_Resources/ExtPages/UploadService.aspx?action=DownLoad&id=");
                                  jsonHelper.AddItem("name", strFileName);
                                  jsonHelper.AddItem("uploader", CommonMethods.GetUserName(account));
                                  jsonHelper.AddItem("filesize", fileLength.ToString());
                                  jsonHelper.ItemOk();

                              }
                              catch
                              {

                                  jsonHelper.AddItem("status_error", "OK");
                                  jsonHelper.ItemOk();
                              }
                          }

                          //string linkName = string.Empty;
                          //string value = AttachmentService.Instance.UploadAttachment(stream, knowledge_id, strFileName, out linkName, fields);
                          ////防止頁(yè)面出現(xiàn)id沖突
                          //jsonHelper.AddItem("status_error", "OK");
                          //jsonHelper.AddItem("Id", value);
                          //jsonHelper.AddItem("fileId", "KBA" + value);
                          //jsonHelper.AddItem("url", linkName);
                          //jsonHelper.AddItem("name", strFileNameNoExt + strExtension);
                          //jsonHelper.AddItem("ext", strExtension);
                          //jsonHelper.AddItem("filesize", fileLength.ToString());
                          //jsonHelper.AddItem("uploader", CommonMethods.GetUserName(account));
                          //jsonHelper.AddItem("fileLastV", currentVer.ToString());

                          //jsonHelper.ItemOk();
                      }

                      strJson = jsonHelper.ToString();
                  }
              }
          }

          posted on 2011-09-16 16:48 王豪 閱讀(449) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          常用鏈接

          留言簿

          隨筆分類(lèi)

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 双桥区| 青龙| 崇明县| 民丰县| 陆河县| 尉氏县| 灵石县| 禹城市| 博湖县| 伊金霍洛旗| 三都| 文安县| 房山区| 手游| 三河市| 庆阳市| 万山特区| 日喀则市| 潮州市| 汤阴县| 涟水县| 东乌珠穆沁旗| 兴文县| 吉林省| 安泽县| 富顺县| 永新县| 皮山县| 会昌县| 武胜县| 遵化市| 宜黄县| 余庆县| 时尚| 梨树县| 金门县| 武城县| 平顶山市| 九寨沟县| 皋兰县| 喀什市|