Ejemplo n.º 1
0
        public string saveFile(out string msg, string filetype = "jpg")
        {
            msg = "";
            FileStream outfile = null;

            try
            {
                DbConnection conn       = CyDbConnManager.GetActiveDbConnection("CRMDB");
                CyQuery      query      = new CyQuery(conn);
                DateTime     serverTime = CyDbSystem.GetDbServerTime(query);
                switch (type)
                {
                case "text":
                    //dir += "Texts";
                    dir = "";
                    break;

                case "image":
                    dir += "Images\\" + serverTime.Date + "\\";
                    break;

                case "voice":
                    dir += "Voices\\" + serverTime.Date + "\\";
                    break;

                case "video":
                    dir += "Videos\\" + serverTime.Date + "\\";
                    break;

                case "news":
                    dir += "News" + serverTime.Date + "\\";
                    break;
                }
                if (dir == "")
                {
                    return("");
                }
                filename     = serverTime.Ticks.ToString() + Convert.ToInt32(((new Random()).NextDouble() * 1000)).ToString() + "." + filetype;
                fullFileName = dir + filename;
                outfile      = new FileStream(fullFileName, FileMode.Create);
                for (int i = 0; i < buffer.Count; i++)
                {
                    outfile.Write(buffer[i], 0, buffer[i].Length);
                }
                return(filename);
            }
            catch (Exception e)
            {
                throw new Exception("文件上传失败!");
            }
            finally
            {
                outfile.Flush();
                outfile.Close();
            }
        }
Ejemplo n.º 2
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/html; charset=utf-8";

            string dir         = context.Server.MapPath("~"); //项目根目录
            string requestType = "";                          //哪个相关操作  比如 用户信息  组信息  后期用在基类中的字段代替

            if (context.Request["requestType"] != null)
            {
                requestType = context.Request["requestType"].ToString();
            }
            if (context.Request["filename"] != null)
            {
                //postData 可以在前台定义好与类对应的格式,并将URL 和 Type设置好
                filename = context.Request["filename"].ToString();
            }
            if (context.Request["localUrl"] != null)
            {
                filename = context.Request["localUrl"];
                filename = filename.Substring(filename.LastIndexOf("\\") + 1);
            }
            //返回提示
            ErrMsg NoteObject = new ErrMsg();

            if (requestType.Length > 0)
            {
                DbConnection conn       = CyDbConnManager.GetActiveDbConnection("CRMDB");
                CyQuery      query      = new CyQuery(conn);
                DateTime     serverTime = CyDbSystem.GetDbServerTime(query);
                query.SQL.Text = "select IP_NET,PSWD,IP_PUB,DIR from FTPCONFIG WHERE ID=1";
                query.Open();
                while (!query.Eof)
                {
                    saveIp = query.FieldByName("IP_PUB").AsString;
                    sPSWD  = query.FieldByName("PSWD").AsString;
                    string FTPDIR = query.FieldByName("DIR").AsString;
                    int    t      = FTPDIR.LastIndexOf("@");
                    sUSER = FTPDIR.Substring(0, t);
                    //sURL = "ftp://" + FTPDIR.Substring(t + 1, FTPDIR.Length - t - 1);
                    sURL = FTPDIR.Substring(t + 1, FTPDIR.Length - t - 1);
                    //this.ftpuser =
                    //obj.sDIR = query.FieldByName("DIR").AsString;
                    //obj.sIP_NET = query.FieldByName("IP_NET").AsString;

                    query.Next();
                }
                query.Close();
                string year        = serverTime.Year.ToString();
                string month       = (serverTime.Month.ToString().CompareTo("10") > 0) ? serverTime.Month.ToString() : 0 + serverTime.Month.ToString();
                string day         = (serverTime.Day.ToString().CompareTo("10") > 0) ? serverTime.Day.ToString() : 0 + serverTime.Day.ToString();
                string FtpsavePath = "WXFtp" + year + month + day;
                #region  此处为FTP上传
                //将文件上传到ftp
                string   savePath = dir + filename;
                FileInfo fileNew  = new FileInfo(savePath);
                string   msg      = string.Empty;
                //ftp不存在此目录
                if (!ftpIsExistsFile(out msg, FtpsavePath, sURL, sUSER, sPSWD))
                {
                    if (msg.Length == 0)
                    {
                        //创建ftp目录
                        MakeDir(out msg, FtpsavePath, sURL, sUSER, sPSWD);
                        if (msg.Length > 0)
                        {
                            NoteObject.errCode    = 1;
                            NoteObject.errMessage = "ftp目录:" + msg;
                            context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                            return;
                        }
                    }
                }
                //上传文件上传不成功报错
                if (!UploadFile(out msg, fileNew, FtpsavePath, sURL, sUSER, sPSWD))
                {
                    NoteObject.errCode    = 2;
                    NoteObject.errMessage = "ftp上传文件:" + msg;
                    context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                    return;
                }
                else
                {
                    NoteObject.result = "http://" + saveIp + "/" + FtpsavePath + "/" + filename.Substring(filename.LastIndexOf("/") + 1);
                    context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                    return;
                }
                #endregion
            }
            else
            {
                //保存公网IP
                saveIp = context.Request.QueryString["sPath"];
                //存在服务器FTP目录
                string FtpsavePath = "FtpImg";
                //文件基础名称
                filename = context.Request.QueryString["sName"];
                //ftp地址  密码 及用户名
                sURL  = context.Request.QueryString["sURL"].ToString().Trim();
                sPSWD = context.Request.QueryString["sPSWD"].ToString().Trim();
                sUSER = context.Request.QueryString["sUSER"].ToString().Trim();
                //取出file上传文件
                HttpPostedFile file = context.Request.Files[0];
                //取本地路径,先将文件上传到服务器(程序服务器)
                string mapPath = context.Server.MapPath("~");
                string path    = mapPath + "\\FtpImg\\";
                if (file != null && file.ContentLength > 0)
                {
                    //form本地上传缓存解决了file取路径浏览器安全导致问题
                    #region  此处先上传本地缓存本地可进行预览(如加预览功能需将ftp上传和本地上传分开写)
                    int    imagesKindInx = file.FileName.LastIndexOf(".");
                    string fileNewName   = ConvertDateTimeInt(DateTime.Now) + filename + file.FileName.Substring(imagesKindInx, file.FileName.Length - imagesKindInx);
                    string savePath      = path + fileNewName;
                    //先删除本地目录及目录下文件(清除文件缓存)
                    try
                    {
                        Directory.Delete(path, true);
                    }
                    catch
                    {
                        try
                        {
                            Directory.CreateDirectory(path);
                        }
                        catch (Exception e)
                        {
                            NoteObject.errCode    = 1;
                            NoteObject.errMessage = "文件目录创建:" + e.Message;
                            context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                        }
                    }
                    try
                    {
                        //本地目录不存在创建
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        //本地命名重复存在删除
                        if (File.Exists(savePath))
                        {
                            File.Delete(savePath);
                        }
                        //保存文件在本地
                        file.SaveAs(savePath);
                    }
                    catch (Exception e)
                    {
                        NoteObject.errCode    = 1;
                        NoteObject.errMessage = "文件提交本地目录:" + e.Message;
                        context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                    }
                    #endregion

                    #region  此处为FTP上传
                    //将文件上传到ftp
                    FileInfo fileNew = new FileInfo(savePath);
                    string   msg     = string.Empty;
                    //ftp不存在此目录
                    if (!ftpIsExistsFile(out msg, FtpsavePath, sURL, sUSER, sPSWD))
                    {
                        if (msg.Length == 0)
                        {
                            //创建ftp目录
                            MakeDir(out msg, FtpsavePath, sURL, sUSER, sPSWD);
                            if (msg.Length > 0)
                            {
                                NoteObject.errCode    = 1;
                                NoteObject.errMessage = "ftp目录:" + msg;
                                context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                                return;
                            }
                        }
                    }
                    //上传文件上传不成功报错
                    if (!UploadFile(out msg, fileNew, FtpsavePath, sURL, sUSER, sPSWD))
                    {
                        NoteObject.errCode    = 2;
                        NoteObject.errMessage = "ftp上传文件:" + msg;
                        context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                        return;
                    }
                    else
                    {
                        NoteObject.result = "http://" + saveIp + "/" + FtpsavePath + "/" + fileNewName;
                        context.Response.Write(JsonConvert.SerializeObject(NoteObject));
                        return;
                    }
                    #endregion
                }
            }
        }
Ejemplo n.º 3
0
        //文件流 保存到本地服务器当中
        public string saveFileResource(out string msg, string filetype = "jpg", string type = "")
        {
            msg = "";
            if (buffer.Count == 0)
            {
                return("");
            }
            FileStream outfile = null;

            try
            {
                DbConnection conn       = CyDbConnManager.GetActiveDbConnection("CRMDB");
                CyQuery      query      = new CyQuery(conn);
                DateTime     serverTime = CyDbSystem.GetDbServerTime(query);
                if (type == "thumb" && filetype == "jpg")
                {
                    filetype = "news";
                }
                string year    = serverTime.Year.ToString();
                string month   = (serverTime.Month.ToString().CompareTo("10") > 0) ? serverTime.Month.ToString() : 0 + serverTime.Month.ToString();
                string day     = (serverTime.Day.ToString().CompareTo("10") > 0) ? serverTime.Day.ToString() : 0 + serverTime.Day.ToString();
                string dirname = year + month + day;
                Path = "\\";//显示图片时使用绝对路径
                string subdir = "WX_Resources\\";
                switch (filetype.ToLower())
                {
                case "text":
                    //dir += "Texts";

                    dir = "";
                    break;

                case "jpg":
                    subdir += "Images\\" + dirname + "\\";    //本地绝对路径
                    //保存到数据库本地
                    break;

                case "amr":
                case "mp3":
                    subdir += "Voices\\" + dirname + "\\";
                    break;

                case "mp4":
                    subdir += "Videos\\" + dirname + "\\";
                    break;

                case "news":
                    subdir  += "News\\" + dirname + "\\";   //图文消息的素材缩略图保存
                    filetype = "jpg";
                    break;
                }
                if (dir == "")
                {
                    return("");
                }
                Directory.CreateDirectory(dir + subdir);
                filename     = serverTime.Ticks.ToString() + Convert.ToInt32(((new Random()).NextDouble() * 1000)).ToString() + "." + filetype;
                fullFileName = dir + subdir + filename;
                outfile      = new FileStream(fullFileName, FileMode.Create);
                for (int i = 0; i < buffer.Count; i++)
                {
                    outfile.Write(buffer[i], 0, buffer[i].Length);
                }
                //素材详细保存到数据库本地
                Path += subdir;
                Path += filename;
                if (buffer.Count == 0)
                {
                    return("");
                }
                return(fullFileName);//
            }
            catch (Exception e)
            {
                throw new Exception("文件上传失败!");
            }
            finally
            {
                outfile.Flush();
                outfile.Close();
            }
        }
Ejemplo n.º 4
0
        //上传文件(1、读取文件,将文件转换成流写入到请求流当中。2、发送请求,获得FTP服务器的返回信息)
        public bool UploadFileToFTP(out string msg, HttpContext context = null)
        {
            msg = "";
            bool result = false;

            try
            {
                if (GetBuffer(out msg, context))
                {
                    string name = saveFileResource(out msg);

                    ;//写到程序所指定的文件夹当中
                    DbConnection conn       = CyDbConnManager.GetActiveDbConnection("CRMDB");
                    CyQuery      query      = new CyQuery(conn);
                    DateTime     serverTime = CyDbSystem.GetDbServerTime(query);
                    query.SQL.Text = "select IP_NET,PSWD,IP_PUB,DIR from FTPCONFIG WHERE ID=1";
                    query.Open();
                    while (!query.Eof)
                    {
                        wxserverurl = query.FieldByName("IP_PUB").AsString;
                        this.ftppwd = query.FieldByName("PSWD").AsString;
                        string FTPDIR = query.FieldByName("DIR").AsString;
                        int    t      = FTPDIR.LastIndexOf("@");
                        this.ftpuser = FTPDIR.Substring(0, t);
                        this.ftpurl  = "ftp://" + FTPDIR.Substring(t + 1, FTPDIR.Length - t - 1);
                        //this.ftpuser =
                        //obj.sDIR = query.FieldByName("DIR").AsString;
                        //obj.sIP_NET = query.FieldByName("IP_NET").AsString;

                        query.Next();
                    }
                    query.Close();
                    //wxserverurl = ConfigurationManager.AppSettings["WXServerIP"].ToString();
                    //this.ftpurl = ConfigurationManager.AppSettings["WXServerFTP"].ToString();
                    //this.ftpuser = ConfigurationManager.AppSettings["ftpuser"].ToString();
                    //this.ftppwd = ConfigurationManager.AppSettings["ftppwd"].ToString();

                    string year  = serverTime.Year.ToString();
                    string month = (serverTime.Month.ToString().CompareTo("10") > 0) ? serverTime.Month.ToString() : 0 + serverTime.Month.ToString();
                    string day   = (serverTime.Day.ToString().CompareTo("10") > 0) ? serverTime.Day.ToString() : 0 + serverTime.Day.ToString();
                    dirname = "WXFtp//" + year + month + day;
                    FtpMakeDir("WXFtp");
                    FtpMakeDir(dirname);

                    Url = ftpurl + "//" + dirname + "//" + filename.Substring(filename.LastIndexOf("/") + 1);

                    Uri           uri        = new Uri(Url);
                    FtpWebRequest ftprequest = (FtpWebRequest)FtpWebRequest.Create(uri);
                    ftprequest.Method = WebRequestMethods.Ftp.UploadFile;//设置操作类型
                    //STOR <filename>储存(复制)文件到服务器上
                    //STOU <filename>储存文件到服务器名称上
                    ftprequest.Credentials = new NetworkCredential(ftpuser, ftppwd);//设置验证

                    //与FTP交互有关的类
                    FtpState state = new FtpState();
                    if (name == "")//属于只传了个文件绝对路径过来
                    {
                        state.FileName = this.dir + this.filename;
                    }
                    else//传的是文件流,并且已经保存到了本地
                    {
                        state.FileName = fullFileName;
                    }
                    //state.FileName = fullFileName;

                    state.Request = ftprequest;

                    // Asynchronously get the stream for the file contents.  //将要上传的内容写到请求流当中
                    ftprequest.BeginGetRequestStream(
                        new AsyncCallback(EndGetStreamCallback),//委托  ,相当于事件处理函数
                        state);


                    waitObject = state.OperationComplete;//用于线程控制

                    // Block the current thread until all operations are complete.

                    waitObject.WaitOne();//直接收到信号,才开始进程

                    // The operations either completed or threw an exception.
                    if (state.OperationException != null)
                    {
                        msg    = state.OperationException.Message;
                        result = false;
                    }
                    result = true;
                }
                else if (msg != "")
                {
                    msg    = "上传到服务器失败";
                    result = false;
                }
                else if (msg == "")
                {
                    result = true;
                }
            }
            catch (Exception e)
            {
                msg    = e.Message;
                result = false;
            }
            return(result);
        }
Ejemplo n.º 5
0
        //public class WX_MenuContentList
        //{
        //    public string name { get; set; }
        //    public string type { get; set; }
        //    public string nbdm { get; set; }
        //    public string url { get; set; }
        //    public string dm;
        //    public List<WX_MenuContentListItem> sub_button { get; set; }
        //}
        //public class WX_MenuContentListItem
        //{
        //    public string type { get; set; }
        //    public string name { get; set; }
        //    public string nbdm { get; set; }
        //    public string url { get; set; }
        //}
        public WX_MenuContent Get_WX_PostMenu(out string msg, string PUBLICID)
        {
            msg = string.Empty;
            WX_MenuContent tp_menucontent = new WX_MenuContent();

            DbConnection conn = CyDbConnManager.GetDbConnection("CRMDB");

            try { conn.Open(); }
            catch (Exception e)
            {
                throw new MyDbException(e.Message, true);
            }
            try
            {
                CyQuery query = new CyQuery(conn);
                List <WX_MenuContentList> menucontentlist = new List <WX_MenuContentList>();
                tp_menucontent.button = menucontentlist;
                List <WX_MenuMiddle> menu = new List <WX_MenuMiddle>();
                try
                {
                    string DbSystemName = CyDbSystem.GetDbSystemName(query.Connection);
                    query.SQL.Clear();
                    query.Params.Clear();
                    query.SQL.Add("select NAME,ASKID,DM,TYPE,URL,PUBLICID from WX_MENU where ").Add(CyDbSystem.GetDataLengthFuncName(conn)).Add("(DM)=2 and PUBLICID='" + PUBLICID + "' order by DM");
                    //query.ParamByName("PUBLICID").AsInteger = Convert.ToInt32(PUBLICID);
                    query.Open();
                    while (!query.Eof)
                    {
                        WX_MenuMiddle tp_menu = new WX_MenuMiddle();
                        menu.Add(tp_menu);

                        tp_menu.key      = Convert.ToString(query.FieldByName("ASKID").AsInteger);
                        tp_menu.dm       = query.FieldByName("DM").AsString;
                        tp_menu.type     = query.FieldByName("TYPE").AsInteger;
                        tp_menu.url      = query.FieldByName("URL").AsString;
                        tp_menu.PUBLICID = query.FieldByName("PUBLICID").AsInteger;
                        if (DbSystemName == "ORACLE")
                        {
                            tp_menu.name = query.FieldByName("NAME").AsString;
                        }
                        else if (DbSystemName == "SYBASE")
                        {
                            tp_menu.name = query.FieldByName("NAME").GetChineseString(200);
                        }
                        query.Next();
                    }
                    query.Close();
                    query.Params.Clear();
                    for (int i = 0; i < menu.Count; i++)
                    {
                        WX_MenuContentList tp_menucontentlist = new WX_MenuContentList();
                        menucontentlist.Add(tp_menucontentlist);
                        tp_menucontentlist.name = menu[i].name;
                        if (menu[i].type == 0)
                        {
                            List <WX_MenuContentListItem> list_menucontentlistitem = new List <WX_MenuContentListItem>();
                            tp_menucontentlist.sub_button = list_menucontentlistitem;
                            query.Params.Clear();
                            query.SQL.Clear();
                            query.SQL.Add("select NAME,TYPE,ASKID,DM,URL,PUBLICID from WX_MENU where " + CyDbSystem.GetDataLengthFuncName(conn) + "(DM)=4 and DM LIKE '" + menu[i].dm + "%" + "' and PUBLICID='" + PUBLICID + "'  order by DM");
                            //query.ParamByName("PUBLICID").AsInteger = Convert.ToInt32(PUBLICID);
                            query.Open();
                            while (!query.Eof)
                            {
                                WX_MenuContentListItem tp_menucontentlistitem = new WX_MenuContentListItem();
                                list_menucontentlistitem.Add(tp_menucontentlistitem);
                                int itemtype = query.FieldByName("TYPE").AsInteger;
                                tp_menucontentlistitem.PUBLICID = query.FieldByName("PUBLICID").AsInteger;
                                if (DbSystemName == "ORACLE")
                                {
                                    tp_menucontentlistitem.name = query.FieldByName("NAME").AsString;
                                }
                                else if (DbSystemName == "SYBASE")
                                {
                                    tp_menucontentlistitem.name = query.FieldByName("NAME").GetChineseString(200);
                                }
                                if (itemtype == 2)
                                {
                                    tp_menucontentlistitem.type = "view";
                                    tp_menucontentlistitem.url  = query.FieldByName("URL").AsString;
                                }
                                if (itemtype == 1)
                                {
                                    tp_menucontentlistitem.type = "click";
                                    tp_menucontentlistitem.key  = Convert.ToString(query.FieldByName("ASKID").AsInteger);
                                }
                                query.Next();
                            }
                            query.Close();
                        }
                        else if (menu[i].type == 1)
                        {
                            tp_menucontentlist.type = "click";
                            tp_menucontentlist.key  = menu[i].key;
                        }
                        else if (menu[i].type == 2)
                        {
                            tp_menucontentlist.type = "view";
                            tp_menucontentlist.url  = menu[i].url;
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e is MyDbException)
                    {
                        throw e;
                    }
                    else
                    {
                        throw new MyDbException(e.Message, query.SqlText);
                    }
                }
            }
            finally
            {
                conn.Close();
            }
            return(tp_menucontent);
        }