Exemple #1
0
        private void downloadFile(object rowIndex)
        {
            int    BufferLen      = 4096;
            string folder         = folderBrowserDialog1.SelectedPath;
            string file           = this.dgvShareFile.Rows[(int)rowIndex].Cells["FilePath"].Value.ToString();
            string fileName       = Path.GetFileName(file);
            string fileServiceUrl = string.Empty;

            using (BugTraceEntities zentity = new BugTraceEntities(EntityContextHelper.GetEntityConnString()))
            {
                var currVersion = zentity.SYS_Version.Where(p => p.IsDefault == 1).FirstOrDefault();
                fileServiceUrl = currVersion.FileServiceUrlPort;
            }
            Stream sourceStream = null;

            try
            {
                EndpointAddress address = new EndpointAddress("http://" + fileServiceUrl + "/JAJ.WinServer/FileService");
                FileTransferSvc.FileServiceClient _client = new FileTransferSvc.FileServiceClient("BasicHttpBinding_IFileService", address);
                sourceStream = _client.DowndloadFile(file);
            }
            catch (Exception ex)
            {
                MyLog.LogError("文件下载失败!", ex);
                return;
            }
            FileStream targetStream = null;

            if (!sourceStream.CanRead)
            {
                MyLog.LogError("下载异常", new Exception("Invalid Stream!"));
                throw new Exception("Invalid Stream!");
            }

            string filePath = Path.Combine(folder, fileName);

            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                byte[] buffer = new byte[BufferLen];
                int    count  = 0;
                while ((count = sourceStream.Read(buffer, 0, BufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                }
                targetStream.Close();
                sourceStream.Close();
            }
            this.Invoke(new MethodInvoker(() => {
                try
                {
                    wait.Close();
                }
                catch
                { }

                MessageBox.Show("下载成功!");
            }));
        }
        private void btnDownLoad_Click(object sender, EventArgs e)
        {
            string file = @"C:\Users\jiaoanjian\Desktop\java文档\ERPV4基础平台调整需求说明书-V0.5.doc";

            var        downloadFolder = System.Environment.CurrentDirectory + "\\DownloadFile";
            string     fileName       = Path.GetFileName(file);
            Stream     sourceStream   = _client.DowndloadFile(file);
            FileStream targetStream   = null;

            if (!sourceStream.CanRead)
            {
                throw new Exception("Invalid Stream!");
            }
            if (!Directory.Exists(downloadFolder))
            {
                Directory.CreateDirectory(downloadFolder);
            }
            string filePath = Path.Combine(downloadFolder, fileName);

            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                byte[] buffer = new byte[BufferLen];
                int    count  = 0;

                while ((count = sourceStream.Read(buffer, 0, BufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                    //if ((int)sourceStream.Length - (int)targetStream.Length > 100000)
                    //{

                    //}
                }
                targetStream.Close();
                sourceStream.Close();
            }
        }
Exemple #3
0
        private void downloadFile(object obj)
        {
            List <KeyValuePair <string, string> > map = obj as List <KeyValuePair <string, string> >;
            int    BufferLen = 4096;
            string folder    = map.Where(p => p.Key == "folder").FirstOrDefault().Value;
            string fileName  = map.Where(p => p.Key == "fileName").FirstOrDefault().Value;
            string uniqueID  = map.Where(p => p.Key == "uniqueID").FirstOrDefault().Value;

            string serverFilePath = "\\UploadFile\\PPM_ProblemTrace\\" + uniqueID + fileName;
            string fileServiceUrl = string.Empty;

            using (BugTraceEntities zentity = new BugTraceEntities(EntityContextHelper.GetEntityConnString()))
            {
                var currVersion = zentity.SYS_Version.Where(p => p.IsDefault == 1).FirstOrDefault();
                fileServiceUrl = currVersion.FileServiceUrlPort;
            }
            Stream sourceStream = null;

            try
            {
                EndpointAddress address = new EndpointAddress("http://" + fileServiceUrl + "/JAJ.WinServer/FileService");
                FileTransferSvc.FileServiceClient _client = new FileTransferSvc.FileServiceClient("BasicHttpBinding_IFileService", address);
                sourceStream = _client.DowndloadFile(serverFilePath);
            }
            catch (Exception ex)
            {
                MyLog.LogError("文件下载失败!", ex);
                this.Invoke(new MethodInvoker(() =>
                {
                    try
                    {
                        wait.Close();
                    }
                    catch
                    { }

                    MessageBox.Show("文件下载失败!");
                }));
                return;
            }

            FileStream targetStream = null;

            if (!sourceStream.CanRead)
            {
                MyLog.LogError("下载异常", new Exception("Invalid Stream!"));
                throw new Exception("Invalid Stream!");
            }

            string filePath = Path.Combine(folder, fileName);

            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                byte[] buffer = new byte[BufferLen];
                int    count  = 0;
                while ((count = sourceStream.Read(buffer, 0, BufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                }
                targetStream.Close();
                sourceStream.Close();
            }
            this.Invoke(new MethodInvoker(() =>
            {
                try
                {
                    wait.Close();
                }
                catch
                { }

                MessageBox.Show("下载成功!");
            }));
        }