/// <summary>
        /// 上传文件,上传文件的进度只能通过时时获取服务端Read的进度
        /// </summary>
        /// <param name="filepath">文件本地路径</param>
        /// <returns>上传成功后返回的文件名</returns>
        public static string UpLoadFile(string filepath, Action <int> action)
        {
            ChannelFactory <IFileTransfer> mfileChannelFactory = null;
            IFileTransfer fileHandlerService = null;

            try
            {
                FileInfo finfo = new FileInfo(filepath);
                if (finfo.Exists == false)
                {
                    throw new Exception("文件不存在!");
                }

                mfileChannelFactory = new ChannelFactory <IFileTransfer>("fileendpoint");
                fileHandlerService  = mfileChannelFactory.CreateChannel();

                UpFile uf = new UpFile();
                if (AppGlobal.cache.Contains("WCFClientID"))
                {
                    uf.clientId = AppGlobal.cache.GetData("WCFClientID").ToString();
                }
                uf.UpKey      = Guid.NewGuid().ToString();
                uf.FileExt    = finfo.Extension;
                uf.FileName   = finfo.Name;
                uf.FileSize   = finfo.Length;
                uf.FileStream = finfo.OpenRead();

                if (action != null)
                {
                    getUpLoadFileProgress(uf.UpKey, action);//获取上传进度条
                }
                UpFileResult result = fileHandlerService.UpLoadFile(uf);

                //mfileChannelFactory.Close();//关闭会话

                if (result.IsSuccess)
                {
                    return(result.Message);
                }
                else
                {
                    throw new Exception("上传文件失败!");
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\n上传文件失败!");
            }
            finally
            {
                if (fileHandlerService != null)
                {
                    (fileHandlerService as IContextChannel).Close();
                }
                if (mfileChannelFactory != null)
                {
                    mfileChannelFactory.Close();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 上传文件,有进度显示
        /// </summary>
        /// <param name="filepath">文件本地路径</param>
        /// <param name="action">进度0-100</param>
        /// <returns>上传成功后返回的文件名</returns>
        public string UpLoadFile(string filepath, Action <int> action)
        {
            IFileTransfer fileHandlerService = null;

            try
            {
                FileInfo finfo = new FileInfo(filepath);
                if (finfo.Exists == false)
                {
                    throw new Exception("文件不存在!");
                }


                fileHandlerService = mfileChannelFactory.CreateChannel();

                UpFile uf = new UpFile();
                uf.clientId   = mConn == null ? "" : mConn.ClientID;
                uf.UpKey      = Guid.NewGuid().ToString();
                uf.FileExt    = finfo.Extension;
                uf.FileName   = finfo.Name;
                uf.FileSize   = finfo.Length;
                uf.FileStream = finfo.OpenRead();

                if (action != null)
                {
                    getupdownprogress(uf.FileStream, uf.FileSize, action);//获取进度条
                }
                UpFileResult result = new UpFileResult();
                result = fileHandlerService.UpLoadFile(uf);

                if (result.IsSuccess)
                {
                    return(result.Message);
                }
                else
                {
                    throw new Exception("上传文件失败!");
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\n上传文件失败!");
            }
            finally
            {
                if (fileHandlerService != null)
                {
                    (fileHandlerService as IContextChannel).Abort();
                }
            }
        }