Exemplo n.º 1
0
        /// <summary>
        /// 上传外部单位附件
        /// </summary>
        /// <param name="para"></param>
        /// <param name="trans"></param>
        /// <param name="objectID"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public string UploadAttachFileToResource(SessionPara para, SQLTransaction trans, string objectID, string content)
        {
            string retStr = "";

            if (!string.IsNullOrEmpty(objectID) && !string.IsNullOrEmpty(content))
            {
                StringBuilder sb           = new StringBuilder();
                var           fileDataList = JsonConvert.DeserializeObject <List <FileInformation> >(content);
                foreach (var file in fileDataList)
                {
                    var newObj = ObjectFactoryUtility.CreateInstance(para, trans, "TESTATTACH");
                    newObj.SetObjectDBValue("ENAME", file.FileName);
                    newObj.SetObjectDBValue("PATH", file.Path);
                    newObj.SetObjectDBValue("MASTERID", objectID);
                    newObj.SetObjectDBValue("UPLOADUSER", para.UserID);
                    newObj.SetObjectDBValue("UPLOADTIME", DatetimeUtil.GetInternalTicksByDateTime(DateTime.Now));
                    newObj.SetObjectDBValue("FILEINFO", JsonConvert.SerializeObject(new List <FileInformation>()
                    {
                        file
                    }));
                    newObj.SaveObject(para, trans);
                    sb.Append(";" + newObj.ObjectID);
                }
                if (sb.Length > 0)
                {
                    retStr = sb.ToString().Substring(1);
                }
            }
            return(retStr);
        }
Exemplo n.º 2
0
        private void UploadFile(HttpContext context)
        {
            string s_rpath  = FileHelper.GetUploadPath();
            string Datedir  = DateTime.Now.ToString("yy-MM-dd");
            string updir    = s_rpath + "\\TransmitFile\\" + Datedir;
            string extname  = string.Empty;
            string fullname = string.Empty;
            string filename = string.Empty;

            if (context.Request.Files.Count > 0)
            {
                try
                {
                    for (int j = 0; j < context.Request.Files.Count; j++)
                    {
                        HttpPostedFile uploadFile = context.Request.Files[j];
                        int            offset     = Convert.ToInt32(context.Request["chunk"]);
                        int            total      = Convert.ToInt32(context.Request["chunks"]);
                        string         name       = context.Request["name"];
                        //文件没有分块
                        if (total == 1 || total == 0)
                        {
                            if (uploadFile.ContentLength > 0)
                            {
                                if (!System.IO.Directory.Exists(updir))
                                {
                                    System.IO.Directory.CreateDirectory(updir);
                                }
                                extname  = System.IO.Path.GetExtension(uploadFile.FileName);
                                filename = System.IO.Path.GetFileName(uploadFile.FileName);
                                string filePath = string.Format("{0}\\{1}", updir, filename);
                                uploadFile.SaveAs(filePath);
                                ProcessFile(filePath, uploadFile.FileName);
                            }
                        }
                        else
                        {
                            //文件 分成多块上传
                            fullname = WriteTempFile(uploadFile, offset);
                            if (total - offset == 1)
                            {
                                //如果是最后一个分块文件 ,则把文件从临时文件夹中移到上传文件 夹中
                                System.IO.FileInfo fi          = new System.IO.FileInfo(fullname);
                                string             oldFullName = string.Format("{0}\\{1}", updir, UploadFileName ?? uploadFile.FileName);
                                System.IO.FileInfo oldFi       = new System.IO.FileInfo(oldFullName);
                                if (oldFi.Exists)
                                {
                                    //文件名存在则删除旧文件
                                    oldFi.Delete();
                                }
                                if (!Directory.Exists(updir))
                                {
                                    Directory.CreateDirectory(updir);
                                }
                                fi.MoveTo(oldFullName);
                                ProcessFile(oldFullName, uploadFile.FileName);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    JObject JResult = new JObject();
                    JResult.Add("State", "F");
                    JResult.Add("Msg", ex.Message);
                    JResult.Add("Stack", ex.StackTrace);
                    JResult.Add("TimeSpan", DatetimeUtil.GetInternalTicksByDateTime(DateTime.Now));
                    string json = JsonConvert.SerializeObject(JResult, new JsonSerializerSettings()
                    {
                        StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
                    });
                    HttpCookie hc = new HttpCookie("TFHResult", json);
                    hc.Expires = DateTime.Now.AddMinutes(10);
                    context.Response.AppendCookie(hc);
                    File.AppendAllText("c:\\inforcenterdata\\TransmitFileHandler.log", Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine + ex.Message + ex.StackTrace);
                    context.Response.StatusCode      = 500;
                    context.Response.ContentEncoding = Encoding.UTF8;
                    context.Response.Write(json);
                }
            }
        }