Esempio n. 1
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="strSystemCode">系统编号</param>
        /// <param name="strModelCode">模块编号</param>
        /// <param name="strFileName">文件名</param>
        /// <param name="strMd5Name">加密文件名</param>
        /// <param name="strID">存储ID(业务ID)</param>
        /// <param name="strGuid">用于真实存储ID(些ID动态产生,用于唯一性)</param>
        /// <param name="data">数据流</param>
        /// <param name="BytesUploaded">已上传量</param>
        /// <param name="dataLength">每次上传文件的大小</param>
        /// <param name="firstChunk">第一块</param>
        /// <param name="lastChunk">最后一块</param>
        /// <param name="strCreateUserID">创建用户ID</param>
        /// <param name="model">model类</param>
        /// <returns></returns>       
        public string SaveUpLoadFile(string strSystemCode, string strModelCode, string strFileName, string strMd5Name, string strID, string strGuid, byte[] data, int BytesUploaded, int dataLength, bool firstChunk, bool lastChunk, string strCreateUserID,UserFile model)
        {
          //  string strPath = string.Format(SavePath, strSystemCode, strModelCode);
            string NewPath = "";
            try
            {
                string strPath = string.Format(model.SavePath, model.CompanyCode, model.SystemCode, model.ModelCode);
                if (!System.IO.Directory.Exists(strPath))
                {
                    if (strPath.IndexOf("\\\\") == 0)
                    {
                        string FirstName = "";
                        string UserName = "";
                        string StrPwd = "";
                        string ComputerName = "";
                        if (StrAuthUser.IndexOf(",") > 0)
                        {
                            FirstName = StrAuthUser.Split(',')[0];
                            ComputerName = FirstName.Split('/')[0];
                            UserName = FirstName.Split('/')[1];
                            StrPwd = StrAuthUser.Split(',')[1];
                        }
                        //LogonImpersonate imper = new LogonImpersonate("user1", "123456");
                        LogonImpersonate imper = new LogonImpersonate(UserName, StrPwd);
                        //WNetHelper.WNetAddConnection(@"liujianxng/user1", "123456", @"//172.16.1.57/fileupload", "Z:");
                        //Directory.CreateDirectory(strPath);
                        string path = @"Z:\" + model.CompanyCode + @"\" + model.SystemCode + @"\" + model.ModelCode;
                        string OldPath = model.SavePath.Split('{')[0].TrimEnd('\\');
                        if (CreateDirectory(path, UserName, StrPwd, OldPath))
                        {
                            SMT.Foundation.Log.Tracer.Debug("创建了驱动器映射");
                        }
                        else
                        {
                            SMT.Foundation.Log.Tracer.Debug("有问题");
                        }
                        //Directory.CreateDirectory(@"Z:\" + model.CompanyCode + @"\" + model.SystemCode + @"\" + model.ModelCode);
                        //file1.SaveAs(@"Z:/newfolder/test.jpg");


                    }
                    else
                    {
                        System.IO.Directory.CreateDirectory(strPath);
                    }
                }
                NewPath = Path.Combine(strPath, strMd5Name); //还未上传完成的路径
                string OldFilePath = Path.Combine(strPath, strFileName); //原来上传的文件
                string PassWordKey = "";//加密字符串
                if (strMd5Name.Length > 8)
                {
                    PassWordKey = strMd5Name.Substring(0, 8);
                }
                else
                {
                    PassWordKey = "12345678";
                }
                if (File.Exists(NewPath))
                {//如果存在,追加内容(续传)
                    using (FileStream fs = File.Open(NewPath, FileMode.Append))
                    {
                        //DecryptFile(fs,strPath, "");
                        fs.Write(data, 0, dataLength);//datalength:文件已上传量
                        fs.Close();
                        fs.Dispose();
                    }
                    using (FileStream fsOld = File.Open(OldFilePath, FileMode.Append))
                    {
                        //DecryptFile(fs,strPath, "");
                        fsOld.Write(data, 0, dataLength);//datalength:文件已上传量
                        fsOld.Close();
                        fsOld.Dispose();
                    }
                }
                else
                {//如果不存在,就创建
                    using (FileStream fs = File.Create(NewPath))
                    {

                        fs.Write(data, 0, data.Length);
                        fs.Close();
                        fs.Dispose();

                    }
                    //创建上传过来的文件
                    using (FileStream fsOld = File.Create(OldFilePath))
                    {

                        fsOld.Write(data, 0, data.Length);
                        fsOld.Close();
                        fsOld.Dispose();

                    }
                }
                if (lastChunk)
                {
                    //string[] strExtension = strMd5Name.Split('.');
                    //string fileName = strID + "." + strExtension[strExtension.Length - 1];
                    string strTempName = Path.Combine(strPath, strMd5Name);//已经上传完成的路径

                    File.Move(NewPath, strTempName);
                    NewPath = strTempName;//最近返回已完成的路径      

                    SMT_FILELIST entity = new SMT_FILELIST();
                    entity.FILENAME = model.FileName;
                    entity.FILESIZE = Convert.ToDecimal(model.FileSize);
                    entity.REMARK = model.Remark;
                    entity.SMTFILELISTID = model.SmtFileListId;// uc.SmtFileListId;//主键ID              
                    entity.FILEURL = model.FileUrl;//文件地址
                    entity.COMPANYCODE = model.CompanyCode;//公司代号
                    entity.COMPANYNAME = model.CompanyName;//公司名字
                    entity.PASSWORD = PassWordKey;//加密字符串
                    //entity.COMPANYNAOWNERDEPARTMENTIDME = model.CompanyName;//公司名字
                    entity.SYSTEMCODE = model.SystemCode;//系统代号
                    entity.MODELCODE = model.ModelCode;//模块代号
                    entity.APPLICATIONID = model.ApplicationID;//业务ID
                    entity.THUMBNAILURL = model.ThumbnailUrl;//缩略图地址
                    entity.FILETYPE = model.FileType;
                    entity.INDEXL = model.INDEXL;//排序
                    entity.REMARK = model.Remark;//备注
                    entity.CREATETIME = model.CreateTime;//创建时间
                    entity.CREATENAME = model.CreateName;//创建人
                    entity.UPDATETIME = model.UpdateTime;//修改时间
                    entity.UPDATENAME = model.UpdateName;//修改人
                    entity.OWNERCOMPANYID = model.OWNERCOMPANYID;
                    entity.OWNERDEPARTMENTID = model.OWNERDEPARTMENTID;
                    entity.OWNERPOSTID = model.OWNERPOSTID;
                    
                    entity.OWNERID = model.OWNERID;
                    entity.FILEURL = string.Format("\\{0}\\{1}\\{2}", model.CompanyCode, model.SystemCode, model.ModelCode + "\\" + strMd5Name);
                    
                    string imageType = ".JPEG,.JPG,.GIF,.BMP";
                    //判断上传的模块的类型,如果是新闻则生成缩略图

                    if (imageType.IndexOf(entity.FILETYPE) > 0 || imageType.ToLower().IndexOf(entity.FILETYPE) > 0)
                    {
                            
                        string thumbPath = string.Format(model.SavePath, model.CompanyCode, model.SystemCode, model.ModelCode)+"\\thumb\\";
                        if (!System.IO.Directory.Exists(thumbPath))
                        {
                            if (strPath.IndexOf("\\\\") == 0)
                            {
                                string FirstName = "";
                                string UserName = "";
                                string StrPwd = "";
                                string ComputerName = "";
                                if (StrAuthUser.IndexOf(",") > 0)
                                {
                                    FirstName = StrAuthUser.Split(',')[0];
                                    ComputerName = FirstName.Split('/')[0];
                                    UserName = FirstName.Split('/')[1];
                                    StrPwd = StrAuthUser.Split(',')[1];
                                }
                                //LogonImpersonate imper = new LogonImpersonate("user1", "123456");
                                LogonImpersonate imper = new LogonImpersonate(UserName, StrPwd);
                                //WNetHelper.WNetAddConnection(@"liujianxng/user1", "123456", @"//172.16.1.57/fileupload", "Z:");
                                //Directory.CreateDirectory(strPath);
                                string path = @"Z:\" + model.CompanyCode + @"\" + model.SystemCode + @"\" + model.ModelCode;
                                string OldPath = model.SavePath.Split('{')[0].TrimEnd('\\');
                                if (CreateDirectory(path, UserName, StrPwd, OldPath))
                                {
                                    SMT.Foundation.Log.Tracer.Debug("创建了驱动器映射");
                                }
                                else
                                {
                                    SMT.Foundation.Log.Tracer.Debug("有问题");
                                }
                                //Directory.CreateDirectory(@"Z:\" + model.CompanyCode + @"\" + model.SystemCode + @"\" + model.ModelCode);
                                //file1.SaveAs(@"Z:/newfolder/test.jpg");


                            }
                            else
                            {
                                System.IO.Directory.CreateDirectory(thumbPath);
                            }
                        }
                        string thumbFile = thumbPath + strMd5Name;
                        string strType = "JPG"; 
                        strType = entity.FILETYPE.Substring(1, entity.FILETYPE.Length-1).ToUpper();
                        MakeImageThumb.MakeThumbnail(OldFilePath, thumbFile, 250, 200, "DB", strType);
                        //保存到数据库的路径
                        string strSaveThumb = string.Format("\\{0}\\{1}\\{2}", model.CompanyCode, model.SystemCode, model.ModelCode + "\\thumb\\" + strMd5Name);
                        //entity.THUMBNAILURL = string.Format("\\{0}\\{1}\\{2}", model.CompanyCode, model.SystemCode, model.ModelCode + "\\" + strMd5Name);
                        entity.THUMBNAILURL = strSaveThumb;
                    }
                    

                    dal.AddEntity(entity);
                    //DecryptFile(NewPath);
                    //Encrypt(NewPath, PassWordKey);
                    //FileStream filOutStream = new FileStream(NewfilePath, FileMode.OpenOrCreate, FileAccess.Write);
                    //InputFileStream = File.OpenWrite(FileName);
                    CryptoHelp.EncryptFile(OldFilePath, NewPath, PassWordKey);
                    //Decrypt(NewPath);
                    //model.FileUrl = string.Format("\\{0}\\{1}\\{2}", model.CompanyCode, model.SystemCode, model.ModelCode +"\\"+ fileName);
                    //dal.Add(model);

                }
                
            }
            catch (Exception ex)
            {                
                SMT.Foundation.Log.Tracer.Debug("model.SavePath:" +model.SavePath);
                SMT.Foundation.Log.Tracer.Debug("model.CompanyCode:" +model.CompanyCode);
                SMT.Foundation.Log.Tracer.Debug("model.SystemCode:" +model.SystemCode);
                SMT.Foundation.Log.Tracer.Debug("model.ModelCode:" +model.ModelCode);
                SMT.Foundation.Log.Tracer.Debug("调用上传文件出错:"+ex.ToString());
            }
            return NewPath;
        }
Esempio n. 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //UploadService bb = new UploadService();
            //bb.CreateCompanyDirectory("703dfb3c-d3dc-4b1d-9bf0-3507ba01b716", "集团本部", "SMT");  
            SMT.Foundation.Log.Tracer.Debug("开始上传文件时间:"+System.DateTime.Now.ToString());
            try
            {
                string NewPath = "";
                HttpPostedFile fileData = Request.Files["Filedata"];
                SMT.Foundation.Log.Tracer.Debug("开始上传文件的大小:" + fileData.ContentLength.ToString());
                SMT.Foundation.Log.Tracer.Debug("开始上传文件:" + fileData.FileName);
                string filePath = "";
                string companyCode = "";//公司代码
                string systemCode = "";//系统代码
                string modelCode = "";//模块代码
                string encryptName = "";//加密字符串
                string strFileName = "";
                strFileName = fileData.FileName;
                UserFile model = null;

                string DownloadUrl = string.Concat(ConfigurationManager.AppSettings["DownLoadUrl"]);
                //获取用户身份,由用户名,密码组成
                string StrAuthUser = string.Concat(ConfigurationManager.AppSettings["FileUploadUser"]);
                //if (Request["path"] != null)
                //{
                //    filePath = Request["path"].ToString();
                //    SMT.Foundation.Log.Tracer.Debug("上传文件完成,路径:" + filePath);
                //}
                if (Request["companyCode"] != null)//公司代码
                {
                    companyCode = Request["companyCode"].ToString();
                    model = FileConfig.GetCompanyItem(companyCode);
                    SMT.Foundation.Log.Tracer.Debug("aspx页面上传文件companyCode:" + companyCode);
                }
                if (Request["systemCode"] != null)//系统代码
                {
                    systemCode = Request["systemCode"].ToString();
                    SMT.Foundation.Log.Tracer.Debug("aspx页面上传文件systemcode:" + systemCode);
                }
                if (Request["modelCode"] != null)//模块代码
                {
                    modelCode = Request["modelCode"].ToString();
                    SMT.Foundation.Log.Tracer.Debug("aspx页面上传文件modlecode:" + modelCode);
                }
                if (Request["encryptName"] != null)//加密文件名
                {
                    encryptName = Request["encryptName"].ToString();
                    SMT.Foundation.Log.Tracer.Debug("aspx页面上传文件encryptName:" + encryptName);
                }
                if (fileData != null)
                {
                    //string path = ctx.Server.MapPath(ctx.Request["file"].ToString());
                    string strPath = string.Format(model.SavePath, companyCode, systemCode, modelCode);
                    if (!System.IO.Directory.Exists(strPath))
                    {
                        if (strPath.IndexOf("\\\\") == 0)
                        {
                            string FirstName = "";
                            string UserName = "";
                            string StrPwd = "";
                            string ComputerName = "";
                            if (StrAuthUser.IndexOf(",") > 0)
                            {
                                FirstName = StrAuthUser.Split(',')[0];
                                ComputerName = FirstName.Split('/')[0];
                                UserName = FirstName.Split('/')[1];
                                StrPwd = StrAuthUser.Split(',')[1];
                            }
                            LogonImpersonate imper = new LogonImpersonate(UserName, StrPwd);
                            string path = @"Z:\" + companyCode + @"\" + systemCode + @"\" + modelCode;
                            string OldPath = model.SavePath.Split('{')[0].TrimEnd('\\');
                            if (CreateDirectory(path, UserName, StrPwd, OldPath))
                            {
                                SMT.Foundation.Log.Tracer.Debug("aspx页面创建了驱动器映射");
                            }
                            else
                            {
                                SMT.Foundation.Log.Tracer.Debug("aspx页面有创建映射问题");
                            }

                        }
                        else
                        {
                            System.IO.Directory.CreateDirectory(strPath);
                        }
                    }
                    NewPath = Path.Combine(strPath, encryptName); //还未上传完成的路径
                    string OldFilePath = Path.Combine(strPath, strFileName); //原来上传的文件
                    string PassWordKey = "";//加密字符串
                    if (encryptName.Length > 8)
                    {
                        PassWordKey = encryptName.Substring(0, 8);
                    }
                    else
                    {
                        PassWordKey = "12345678";
                    }
                    int fileLength = fileData.ContentLength;
                    byte[] data = new Byte[fileLength];
                    Stream fileStream = fileData.InputStream;
                    fileStream.Read(data, 0, fileLength);

                    SMT.Foundation.Log.Tracer.Debug("aspx页面上传文件开始,路径NewPath:" + NewPath);
                    using (FileStream fs = File.Create(NewPath))
                    {
                        fs.Write(data, 0, data.Length);
                        fs.Close();
                        fs.Dispose();
                    }
                    //创建上传过来的文件
                    SMT.Foundation.Log.Tracer.Debug("aspx页面上传文件开始,路径OldFilePath:" + OldFilePath);
                    using (FileStream fsOld = File.Create(OldFilePath))
                    {
                        fsOld.Write(data, 0, data.Length);
                        fsOld.Close();
                        fsOld.Dispose();                        
                    }

                    string strTempName = Path.Combine(strPath, encryptName);//已经上传完成的路径
                    SMT.Foundation.Log.Tracer.Debug("aspx页面上传文件开始,路径strTempName:" + strTempName);
                    File.Move(NewPath, strTempName);
                    NewPath = strTempName;//最近返回已完成的路径                 
                    string imageType = ".JPEG,.JPG,.GIF,.BMP";
                    //判断上传的模块的类型,如果是新闻则生成缩略图
                    #region 生成缩略图

                    string FileType = "";
                    if (imageType.IndexOf(FileType) > 0 || imageType.ToLower().IndexOf(FileType) > 0)
                    {
                        string thumbPath = string.Format(model.SavePath, companyCode, systemCode, modelCode) + "\\thumb\\";
                        SMT.Foundation.Log.Tracer.Debug("aspx页面上传图片文件,路径thumbPath:" + thumbPath);
                        if (!System.IO.Directory.Exists(thumbPath))
                        {
                            SMT.Foundation.Log.Tracer.Debug("aspx页面上传图片文件,不存在路径thumbPath:" + thumbPath);
                            if (strPath.IndexOf("\\\\") == 0)
                            {
                                string FirstName = "";
                                string UserName = "";
                                string StrPwd = "";
                                string ComputerName = "";
                                if (StrAuthUser.IndexOf(",") > 0)
                                {
                                    FirstName = StrAuthUser.Split(',')[0];
                                    ComputerName = FirstName.Split('/')[0];
                                    UserName = FirstName.Split('/')[1];
                                    StrPwd = StrAuthUser.Split(',')[1];
                                }
                                LogonImpersonate imper = new LogonImpersonate(UserName, StrPwd);
                                string path = @"Z:\" + companyCode + @"\" + systemCode + @"\" + modelCode;
                                string OldPath = model.SavePath.Split('{')[0].TrimEnd('\\');
                                if (CreateDirectory(path, UserName, StrPwd, OldPath))
                                {
                                    SMT.Foundation.Log.Tracer.Debug("创建了驱动器映射");
                                }
                                else
                                {
                                    SMT.Foundation.Log.Tracer.Debug("aspx页面图片上传有问题");
                                }

                            }
                            else
                            {
                                SMT.Foundation.Log.Tracer.Debug("aspx页面上传图片文件,开始创建路径thumbPath:" + thumbPath);
                                System.IO.Directory.CreateDirectory(thumbPath);
                            }
                        }
                        string thumbFile = thumbPath + encryptName;
                        string strType = "JPG";
                        strType = FileType;
                        MakeImageThumb.MakeThumbnail(OldFilePath, thumbFile, 250, 200, "DB", strType);
                        //保存到数据库的路径
                        string strSaveThumb = string.Format("\\{0}\\{1}\\{2}", companyCode, systemCode, modelCode + "\\thumb\\" + encryptName);
                        //entity.THUMBNAILURL = string.Format("\\{0}\\{1}\\{2}", model.CompanyCode, model.SystemCode, model.ModelCode + "\\" + strMd5Name);
                        //entity.THUMBNAILURL = strSaveThumb;
                    }
                    #endregion
                    CryptoHelp.EncryptFileForMVC(OldFilePath, NewPath, PassWordKey);
                    //fileStream.Close();
                    //fileStream.Dispose();
                    SMT.Foundation.Log.Tracer.Debug("aspx页面上传文件完成,路径:" + NewPath + " tempName:" + strTempName);
                }
            }
            catch (Exception ex)
            {
                SMT.Foundation.Log.Tracer.Debug("上传文件upload.aspx页面出现错误:" + ex.ToString());
            }
        }