//----------------------------------------------- For creating file stream receive end point
        public void upLoadFile(FileTransferMessage msg)
        {
            int totalBytes = 0;

            hrt.Start();
            filename = msg.fileName;
            string rfilename = Path.Combine(savePath, filename);

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            using (var outputStream = new FileStream(rfilename, FileMode.Create))
            {
                while (true)
                {
                    int bytesRead = msg.transferStream.Read(block, 0, BlockSize);
                    totalBytes += bytesRead;
                    if (bytesRead > 0)
                    {
                        outputStream.Write(block, 0, bytesRead);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            hrt.Stop();
            Console.Write("\n  Received file \"{0}\" of {1} bytes in {2} microsec.", filename, totalBytes, hrt.ElapsedMicroseconds);
        }
Exemple #2
0
        void uploadFile(string filename, string ToSendPath, string savePath)

        {
            string fqname = System.IO.Path.Combine(ToSendPath, filename);

            try
            {
                hrt.Start();
                using (var inputStream = new FileStream(System.IO.Path.GetFullPath(fqname), FileMode.Open))
                {
                    FileTransferMessage msg = new FileTransferMessage();
                    msg.filename       = filename;
                    msg.transferStream = inputStream;
                    msg.savePath       = savePath;
                    channel.upLoadFile(msg);
                }

                hrt.Stop();
                Console.Write("\n  Uploaded file \"{0}\" in {1} microsec.", filename, hrt.ElapsedMicroseconds);
            }
            catch
            {
                Console.Write("\n  can't find \"{0}\"", fqname);
            }
        }
Exemple #3
0
        /*
         * Service method to allow user to upload a file
         */
        public static string uploadFile(FileTransferMessage msg)
        {
            Console.Write("UPLOAD FILE");
            HRTimer.HiResTimer hrt = new HRTimer.HiResTimer();
            hrt.Start();
            //savePath += "\\" + msg.username;
            string uploadPath = "";
            int    count      = -1;

            if (msg.uploadAsDirectory)
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(savePath + "\\" + msg.username);
                count      = dir.GetDirectories().Length + 1;
                uploadPath = savePath + "\\" + msg.username + "\\" + count.ToString();
            }
            else
            {
                uploadPath = savePath + "\\" + msg.username;
            }
            string filename  = msg.filename;
            string rfilename = Path.Combine(uploadPath, filename);

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            using (var outputStream = new FileStream(rfilename, FileMode.Create))
            {
                while (true)
                {
                    int bytesRead = msg.transferStream.Read(block, 0, BlockSize);
                    if (bytesRead > 0)
                    {
                        outputStream.Write(block, 0, bytesRead);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            hrt.Stop();
            string xmlName = addToFileMetaData(msg.username, filename, count);

            Console.Write("\n  Received file \"{0}\"", filename);

            //can upload one one file at once
            string[] files       = new string[2];
            string   correctPath = "../../FileStorage/" + msg.username;

            if (msg.uploadAsDirectory)
            {
                correctPath += "/" + count;
            }
            files[0] = correctPath;
            files[1] = filename;
            calculateMaintainibility(files, xmlName);

            return("File successfully uploaded.");
        }
        public void uploadFile(string filename, string toSend)  // calluploader function calls it with name of the files and upload one by one
        {
            int    numRetries   = 10;
            int    waitMilliSec = 50;
            string fqname       = Path.Combine(toSend, filename);
            string x            = Path.GetFullPath(fqname);

            for (int i = 0; i < numRetries; i++)
            {
                try
                {
                    hrt.Start();
                    using (var inputStream = new FileStream(fqname, FileMode.OpenOrCreate))
                    {
                        FileTransferMessage msg = new FileTransferMessage();
                        msg.fileName       = filename;
                        msg.transferStream = inputStream;
                        channel.upLoadFile(msg);
                    }
                    hrt.Stop();
                    Console.Write("\n  Uploaded file \"{0}\" in {1} microsec.", filename, hrt.ElapsedMicroseconds);
                    break;
                }
                catch (Exception ex)
                {
                    Thread.Sleep(waitMilliSec);
                    Console.Write("\n  Error: \"{0}\"", ex.Message);
                    Console.WriteLine("\nRetry attempt: {0}", i + 1);
                }
            }
        }
        public void uploadFile(string filename) // calluploader function calls it with name of the files and upload one by onee
        {
            int numRetries   = 10;
            int waitMilliSec = 50;

            string fqname = Path.Combine(sourceDLL, filename);
            string x      = Path.GetFullPath(fqname);

            for (int i = 0; i < numRetries; i++)  // retry 10 times incase of a failure
            {
                try
                {
                    hrt.Start();
                    using (var inputStream = new FileStream(fqname, FileMode.Open))
                    {
                        FileTransferMessage msg = new FileTransferMessage();
                        msg.fileName       = filename;
                        msg.transferStream = inputStream;
                        channel.upLoadFile(msg);
                    }
                    hrt.Stop();
                    Console.Write("\n  Uploaded file \"{0}\" in {1} microsec.", filename, hrt.ElapsedMicroseconds);
                    break;
                }
                catch (Exception ex)
                {
                    // Sending files to repository using files Streaming..
                    Thread.Sleep(waitMilliSec);
                    Console.WriteLine("\n  ---------------------Unable to Send Files to the Repository--------------------- \n");
                    Console.Write("\n  Error: \"{0}\"", ex.Message);
                    Console.WriteLine("\nRetry attempt: {0} -->> To Send {1}", i + 1, Path.GetFileName(fqname));
                }
            }
        }
Exemple #6
0
        static void upload(string filename)
        {
            // byte[] block;
            string ToSendPath = "../../../Repository";

            channel = CreateServiceChannel("http://localhost:8000/StreamService");
            byte[] block = new byte[1024];
            //int totalBytes = 0;
            string fqname = System.IO.Path.Combine(ToSendPath, filename);

            try
            {
                //hrt.Start();
                using (var inputStream = new FileStream(filename, FileMode.Open))
                {
                    FileTransferMessage msg = new FileTransferMessage();
                    msg.filename       = filename;
                    msg.transferStream = inputStream;
                    channel.upLoadFile(msg);
                }
                // hrt.Stop();
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
            }
        }
        protected override void OnFileTransferFinished(FileTransferMessage request, RemoteTransferRequest req, bool succeeded, string targetFile)
        {
            base.OnFileTransferFinished(request, req, succeeded, targetFile);
            var res = req.GetResourceInfo(request.GUID);

            Program.Service.MainChannel.ReportDownloadedOneResourceFile(res.FileName);
        }
        // sending files
        public void upLoadFile(FileTransferMessage msg)
        {
            int totalBytes = 0;

            hrt.Start();
            filename = msg.fileName;
            string location;
            int    flag = 0;

            if (filename.Contains(".txt"))
            {
                string dirName = filename;
                dirName = dirName.Split('-')[1];
                string name = Regex.Replace(dirName, "[0-9]", "");
                name = Regex.Replace(name, " ", "");
                string timeStamp = DateTime.Now.ToString("MM-dd-yyyy") + " " + DateTime.Now.Hour + "-" + DateTime.Now.Minute + " " + DateTime.Now.ToString("tt");
                name    += timeStamp;
                location = Path.Combine(savePath_Logs, name);
                flag     = 1;
            }
            else
            {
                location = savePath;
            }

            string rfilename = Path.Combine(location, filename);

            if (!Directory.Exists(location))
            {
                Directory.CreateDirectory(location);
            }
            using (var outputStream = new FileStream(rfilename, FileMode.OpenOrCreate))
            {
                while (true)
                {
                    int bytesRead = msg.transferStream.Read(block, 0, BlockSize);
                    totalBytes += bytesRead;
                    if (bytesRead > 0)
                    {
                        outputStream.Write(block, 0, bytesRead);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            hrt.Stop();

            if (flag == 1)
            {
                (" [[ Displaying Requirement 7 ---->>> Received Logs from the Test Harness Server ]]").title('-');
                Console.Write("\n  Received file \"{0}\" of {1} bytes in {2} microsec.", filename, totalBytes, hrt.ElapsedMicroseconds);
                (" [[ Displaying Requirement 8 ---->>> Stored Logs at Location: " + location + " ]]").title('-');
            }
            else
            {
                Console.Write("\n  Received file \"{0}\" of {1} bytes in {2} microsec.", filename, totalBytes, hrt.ElapsedMicroseconds);
            }
        }
Exemple #9
0
        /*
         * Upload a file chosen by user to be uploadded to server
         */
        private void Upload_File(object sender, RoutedEventArgs e)
        {
            string filename   = FileActionTextBox.Text;
            string ToSendPath = "..\\..\\ToSend";

            Console.Write("\n  sending file \"{0}\"", filename);
            string fqname = System.IO.Path.Combine(ToSendPath, filename);

            using (var inputStream = new FileStream(fqname, FileMode.Open))
            {
                FileTransferMessage msg = new FileTransferMessage();
                msg.filename          = filename;
                msg.transferStream    = inputStream;
                msg.uploadAsDirectory = false;
                msg.username          = authenticatedUser;

                if (UploadCheckBox.IsChecked ?? false)
                {
                    FileActionLabel.Content = "it did the check";
                    msg.uploadAsDirectory   = true;
                }

                channel.uploadFile(msg);
                FileActionLabel.Content = "File " + filename + " uploaded.";
            }
        }
Exemple #10
0
        private void TransferFiles(string ipe, IFileTransmissionService transfer)
        {
            //LogSink.Write(MessageType.文件传输, String.Format("开始向客户端{0}传输文件...", ipe));
            bool succeeded = true;
            int  index     = 0;

            foreach (var kv in m_Files)
            {
                try
                {
                    var msg = new FileTransferMessage();
                    msg.Index   = index++;
                    msg.GUID    = kv.Value.GUID;
                    msg.Content = new FileStream(kv.Key, FileMode.Open, FileAccess.Read, FileShare.Read);
                    transfer.TransferFile(msg);
                    msg.Content.Close();
                }
                catch// (Exception ex)
                {
                    //LogSink.Write(MessageLevel.错误, MessageType.文件传输, String.Format("向客户端{0}传输文件{1}失败!({2})", ipe, kv.Key, ex.Message));
                    succeeded = false;
                }
            }
            //if (succeeded)
            //    LogSink.Write(MessageType.文件传输, String.Format("向客户端{0}传输文件成功!", ipe));
        }
Exemple #11
0
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            IFTService channel;
            string     ToSendPath = "upload";

            int BlockSize = 1024;

            byte[]   block;
            string[] filename = Directory.GetFiles(ToSendPath);

            block   = new byte[BlockSize];
            channel = CreateServiceChannel("http://localhost:8000/StreamService");

            foreach (string x in filename)
            {
                string x1     = System.IO.Path.GetFileName(x);
                string fqname = System.IO.Path.Combine(ToSendPath, x1);
                try
                {
                    using (var inputStream = new FileStream(fqname, FileMode.Open))
                    {
                        FileTransferMessage msg = new FileTransferMessage();
                        msg.filename       = x1;
                        msg.transferStream = inputStream;
                        channel.upLoadFile(msg);
                    }

                    FTLabel.Content = " Uploaded file ";
                }
                catch
                {
                    FTLabel.Content = "Error";
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// 上传附件到指定路径
        /// </summary>
        /// <param name="path">指定服务端路径</param>
        /// <param name="filePath">本地文件路径</param>
        /// <returns></returns>
        public static bool UploadFileByPath(string path, string filePath)
        {
            FileTransferMessage fileMessage = new FileTransferMessage();

            fileMessage.CookieStr = GlobalStaticObj.CookieStr;
            fileMessage.UserID    = GlobalStaticObj.UserID;
            fileMessage.SavePath  = path;
            fileMessage.FileName  = Path.GetFileName(filePath);
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            fileMessage.FileData = fileStream;
            try
            {
                GlobalStaticObj.proxyFile.UploadFile(fileMessage);
                fileStream.Close();
                fileStream.Dispose();
                return(true);
            }
            catch (Exception e)
            {
                fileStream.Close();
                fileStream.Dispose();
                GlobalStaticObj.GlobalLogService.WriteLog("【附件上传】" + e);  //Modify by kord -- Utility.Log.Log.writeLineToLog(e, "附件上传");
                return(false);
            }
        }
        void ClientUploadFile(string filename)
        {
            hrt.Start();
            string fqname = System.IO.Path.Combine(ToSendPath, filename);

            using (var inputStream = new FileStream(fqname, FileMode.Open))
            {
                FileTransferMessage msg = new FileTransferMessage();
                msg.filename       = filename;
                msg.transferStream = inputStream;
                fileProxy          = CreateClientFileServiceChannel("http://localhost:8000/StreamService");
                int i = 3;
                while (i > 0)
                {
                    try
                    {
                        fileProxy.upLoadFile(msg);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("\nfileupload exception: {0}", ex.Message);
                        i--;
                        Thread.Sleep(500);
                    }
                }
            }
            hrt.Stop();
            Console.WriteLine("\n  Uploaded file \"{0}\" in {1} microsec.", filename, hrt.ElapsedMicroseconds);
            listBox2.Items.Insert(0, "uploaded file " + filename + " in " + hrt.ElapsedMicroseconds + " microsec");
        }
Exemple #14
0
        private void lstBoxRepFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            int index = lstBoxRepFiles.SelectedIndex;

            if (index != System.Windows.Forms.ListBox.NoMatches)
            {
                FileTransferMessage msg = new FileTransferMessage();
                msg.Filename = lstBoxRepFiles.Items.GetItemAt(index).ToString();
                msg.Type     = FileTransferMessage.FileType.Library;
                FilesToDownload.enQ(msg);
            }
        }
Exemple #15
0
 /// <summary>Downloads files from repository</summary>
 void DownloadFiles()
 {
     while (true)
     {
         FileTransferMessage file = FilesToDownload.deQ();
         if (file.Filename.ToLower().Equals("quit"))
         {
             break;
         }
         try {
             sender.DownloadFile(file.Filename, file.Type, "Repository");
         } catch (Exception) {
             Console.Write("\n  Unable to download file {0} from the repository", file.Filename);
         }
     }
 }
Exemple #16
0
        /// <summary>
        /// 传输文件
        /// </summary>
        private void btnSendFile_Click(object sender, EventArgs e)
        {
            string filePath = "";

            for (int i = 0; i < dgUploadList.Rows.Count - 1; i++)
            {
                filePath = dgUploadList.Rows[i].Cells[0].Value.ToString();

                if (filePath == string.Empty)
                {
                    MessageBox.Show(@"请选择要传输的文件");
                    return;
                }
                if (_proxy == null)
                {
                    MessageBox.Show(@"服务已经断开");
                    return;
                }

                FileTransferMessage file = null;
                try
                {
                    file = new FileTransferMessage
                    {
                        FileName = Path.GetFileName(filePath),
                        FileData = new FileStream(filePath, FileMode.Open)
                    };

                    //IContextChannel obj = _proxy as IContextChannel;

                    var sendThread = new FileSendThread {
                        _file = file, _proxy = _proxy
                    };
                    var threadRead = new Thread(new ThreadStart(sendThread.SendFile));
                    threadRead.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            MessageBox.Show(@"文件传输成功");
        }
Exemple #17
0
 //uploads file to repository
 public void uploadFileToRepo(string filePath, string filename)
 {
     try
     {
         //hrt.Start();
         using (var inputStream = new FileStream(filePath, FileMode.Open))
         {
             FileTransferMessage msg = new FileTransferMessage();
             msg.filename       = filename;
             msg.transferStream = inputStream;
             Repochannel.upLoadFile(msg);
         }
         // hrt.Stop();
         //Console.Write("\n  Uploaded file \"{0}\" in {1} microsec.", filename, hrt.ElapsedMicroseconds);
     }
     catch (Exception e)
     {
         Console.Write("\n  can't find \"{0}\" exception {1}", filePath, e);
     }
 }
        public static bool UploadFile(Stream stream, string fileName)
        {
            FileTransferMessage fileMessage = new FileTransferMessage();

            fileMessage.CookieStr = GlobalStaticObj.CookieStr;
            fileMessage.UserID    = GlobalStaticObj.UserID;
            //fileMessage.SavePath = filePath;
            fileMessage.FileName = fileName;
            //FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            fileMessage.FileData = stream;
            try
            {
                return(GlobalStaticObj.proxyFile.UploadFile(fileMessage).Flag);
                //GlobalStaticObj.proxyFile.UploadFile1(stream, fileName);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
        void uploadFile(string filename)
        {
            string fqname = Path.Combine(ToSendPath, filename);

            try
            {
                hrt.Start();
                using (var inputStream = new FileStream(fqname, FileMode.Open))
                {
                    FileTransferMessage msg = new FileTransferMessage();
                    msg.filename       = filename;
                    msg.transferStream = inputStream;
                    channel.upLoadFile(msg);
                }
                hrt.Stop();
                Console.Write("\n  Uploaded file \"{0}\" in {1} microsec.", filename, hrt.ElapsedMicroseconds);
            }
            catch (Exception ex)
            {
                Console.Write("\n  can't find the file\"{0}\"------->Requirement #3", fqname);
            }
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public static bool UploadFile(string savePath, string fileName)
        {
            FileTransferMessage fileMessage = new FileTransferMessage();

            fileMessage.CookieStr = GlobalStaticObj.CookieStr;
            fileMessage.UserID    = GlobalStaticObj.UserID;
            fileMessage.SavePath  = savePath;
            fileMessage.FileName  = fileName;
            FileStream fileStream = new FileStream(savePath, FileMode.Open, FileAccess.Read);

            fileMessage.FileData = fileStream;
            try
            {
                GlobalStaticObj.proxyFile.UploadFile(fileMessage);
                fileStream.Close();
                fileStream.Dispose();
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
 protected override void OnTransmissionFinished(FileTransferMessage request, RemoteTransferRequest req)
 {
     base.OnTransmissionFinished(request, req);
     MainForm.Instance.OnTrainingProjectFileReady();
 }
Exemple #22
0
        /// <summary> 上传文件
        /// </summary>
        /// <param name="request">1上传成功,0上传失败</param>
        public UploadFileResponse UploadFile(FileTransferMessage request)
        {
            UploadFileResponse response = new UploadFileResponse();

            response.Flag = false;
            UserFileOPLog uOpLog = new UserFileOPLog();
            UserIDOP      uOp    = new UserIDOP();

            uOp.UserID      = request.UserID;
            uOp.OPName      = "UploadFile";
            uOpLog.userOP   = uOp;
            uOpLog.FileName = request.FileName;

            if (!Directory.Exists(GlobalStaticObj_Server.Instance.FilePath))         //存放的默认文件夹是否存在
            {
                Directory.CreateDirectory(GlobalStaticObj_Server.Instance.FilePath); //不存在则创建
            }
            string path = GlobalStaticObj_Server.Instance.FilePath;

            if (!string.IsNullOrEmpty(request.SavePath))
            {
                path = Path.Combine(GlobalStaticObj_Server.Instance.FilePath, request.SavePath);
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            uOpLog.sTimeTicks = DateTime.UtcNow.Ticks;
            string fileName     = request.FileName;             //文件名
            string fileFullPath = Path.Combine(path, fileName); //合并路径生成文件存放路径

            uOpLog.FilePath = fileFullPath;
            Stream sourceStream = request.FileData;

            if (sourceStream == null)
            {
                return(response);
            }
            if (!sourceStream.CanRead)
            {
                return(response);
            }
            //创建文件流,读取流中的数据生成文件
            using (FileStream fs = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                try
                {
                    const int bufferLength = 4096;
                    byte[]    myBuffer     = new byte[bufferLength];//数据缓冲区
                    int       count;
                    while ((count = sourceStream.Read(myBuffer, 0, bufferLength)) > 0)
                    {
                        fs.Write(myBuffer, 0, count);
                    }
                    fs.Close();
                    sourceStream.Close();
                    uOpLog.eTimeTicks = DateTime.UtcNow.Ticks;
                    uOpLog.exeResult  = true;
                    response.Flag     = true;
                    return(response);
                }
                catch (Exception ex)
                {
                    uOpLog.eTimeTicks = DateTime.UtcNow.Ticks;
                    uOpLog.exeResult  = false;
                    return(response);

                    throw ex;
                }
                finally {
                    writeUserFileOpLog(uOpLog);
                }
            }
        }
Exemple #23
0
        public void TransferFile(FileTransferMessage request)
        {
            _msgContent = string.Format("开始接收文件,name={0}", request.FileName);

            //打印日志
            _info.InitLogMsg(_msgContent);


            //文件信息
            string uploadFolder = AppValue.GetParam()._saveDir;
            string savaPath     = request.SavePath;
            string fileName     = request.FileName;
            Stream sourceStream = request.FileData;

            //判断文件是否可读
            if (!sourceStream.CanRead)
            {
                throw new Exception("数据流不可读!");
            }
            if (savaPath == null)
            {
                savaPath = @"文件传输\";
            }
            if (!savaPath.EndsWith("\\"))
            {
                savaPath += "\\";
            }
            if (!uploadFolder.EndsWith("\\"))
            {
                uploadFolder += "\\";
            }

            uploadFolder = uploadFolder + savaPath;
            //创建保存文件夹
            if (!Directory.Exists(uploadFolder))
            {
                Directory.CreateDirectory(uploadFolder);
            }

            int    fileSize = 0;
            string filePath = Path.Combine(uploadFolder, fileName);//Combine合并两个路径

            try
            {
                //文件流传输
                FileStream targetStream;
                using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    //定义文件缓冲区
                    const int bufferLen = 4096;
                    var       buffer    = new byte[bufferLen];
                    int       count;

                    while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                    {
                        targetStream.Write(buffer, 0, count);
                        fileSize += count;
                    }
                    targetStream.Close();
                    sourceStream.Close();
                }
            }
            catch (Exception ex)
            {
                //打印日志
                _info.InitLogMsg(ex.Message);
            }

            //打印日志
            _msgContent = string.Format("接收文件完毕 name={0},filesize={1}", request.FileName, fileSize);
            _info.InitLogMsg(_msgContent);
        }