Example #1
0
        //Xử lý nhận file dùng StreamReader
        private string DoReciveFile(StreamReader sr)
        {
            string filename = sr.ReadLine();

            lstFileName.Add(filename);
            int numPacket = Convert.ToInt32(sr.ReadLine());

            if (base.InvokeRequired)
            {
                base.Invoke(new Action(delegate()
                {
                    pnlFile.Visible       = true;
                    Cursor                = Cursors.WaitCursor;
                    prgFile.Maximum       = numPacket;
                    prgFile.Value         = 0;
                    FileStream fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
                    for (int i = 0; i < numPacket; i++)
                    {
                        string s      = sr.ReadLine();
                        SendingBuffer = Convert.FromBase64String(s);
                        fileStream.Write(SendingBuffer, 0, SendingBuffer.Length);
                        if (prgFile.Value >= prgFile.Maximum)
                        {
                            prgFile.Value = prgFile.Minimum;
                        }
                        prgFile.PerformStep();
                        lblFile.Text = string.Concat(new object[]
                        {
                            "Đã nhận ",
                            prgFile.Value,
                            "/",
                            prgFile.Maximum
                        });
                    }
                    fileStream.Close();
                    Cursor          = Cursors.Default;
                    pnlFile.Visible = false;
                }));
            }
            string uriString   = Directory.GetCurrentDirectory() + "/" + filename;
            Uri    uri         = new Uri(uriString);
            string absoluteUri = uri.AbsoluteUri;
            string a           = Path.GetExtension(filename).ToLower();

            // Trả về content dạng HTML
            return(string.Concat(new string[]
            {
                "<a href='",
                uri.PathAndQuery.Replace(":", "(~*)"),
                "'>",
                (a == ".jpg" || a == ".png") ? ("<img src='" + absoluteUri + "' style='max-width:300px'/><br/>") : "",
                "<b>",
                filename,
                "</b></a> (",
                FileSize.SizeSuffix(new FileInfo(filename).Length),
                ")"
            }));
        }
Example #2
0
        // Convert nội dung sang dạng html để hiển thị trên webbrowser
        private void SendImage(string path)
        {
            Uri    uri         = new Uri(path);
            string absoluteUri = uri.AbsoluteUri;
            string a           = Path.GetExtension(path).ToLower();
            string content     = string.Concat(new string[]
            {
                "<a href='",
                uri.PathAndQuery.Replace(":", "(~*)"),
                "'>",
                (a == ".jpg" || a == ".png") ? ("<img src='" + absoluteUri + "' style='max-width:300px'/><br/>") : "",
                "<b>",
                Path.GetFileName(path),
                "</b></a> (",
                FileSize.SizeSuffix(new FileInfo(path).Length),
                ")"
            });

            SendMessage(content, path);
        }
Example #3
0
        // Hàm tính toán size của file gửi/nhận từ byte -> các đơn vị khác

        public static string SizeSuffix(long value)
        {
            string result;

            if (value < 0L)
            {
                result = "-" + FileSize.SizeSuffix(-value);
            }
            else if (value == 0L)
            {
                result = "0.0 bytes";
            }
            else
            {
                int     num  = (int)Math.Log((double)value, 1024.0);
                decimal num2 = value / (1L << num * 10);
                result = string.Format("{0:n1} {1}", num2, FileSize.SizeSuffixes[num]);
            }
            return(result);
        }