Esempio n. 1
0
        private void download()
        {
            Filehost.FilehostClient client = new FilehostClient();
            Filehost.DTO            dto    = new DTO();
            Stream stream  = null;
            string oldPath = filePathD + "\\" + tbFileNameD.Text;
            long   curByte = 0;

            try
            {
                stream = new FileStream(oldPath, FileMode.OpenOrCreate, FileAccess.Write);
                do
                {
                    dto             = client.getFile(tbFileNameD.Text, curByte);
                    stream.Position = curByte;
                    stream.Write(dto.Data, 0, dto.Data.Length);
                    curByte += dto.Data.Length;
                    setPos((int)(curByte * 100 / dto.Size), progressBarDownload);
                    dto.Data = null;
                } while (curByte != dto.Size);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (curByte == dto.Size)
                {
                    MessageBox.Show("Файл " + dto.Name + " был успешно загружен!", "Успешно!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                string newPath = filePathD + "\\" + dto.Name;
                int    i       = 1;
                while (File.Exists(newPath))
                {
                    string[] strParse = dto.Name.Split('.');
                    newPath = filePathD + "\\" + strParse[0] + " (" + i.ToString() + ")." + strParse[1];
                }
                File.Move(oldPath, newPath);
                setDefaultTab2();
            }
        }
Esempio n. 2
0
        private void send()
        {
            Stream stream   = null;
            long   lenght   = -1;
            string fileName = Path.GetFileName(fileNameS);
            long   curByte  = 0;

            try
            {
                stream = ofd1.OpenFile();
                lenght = (Int32)stream.Length;

                if (stream != null)
                {
                    const int bufferSize           = 32768;
                    Filehost.FilehostClient client = new FilehostClient();
                    Filehost.DTO            dto    = new DTO();
                    curByte = client.getFileInfo(fileName);

                    if (curByte != 0)
                    {
                        MessageBox.Show("Файл " + fileName + " уже есть на сервере, но не закачан полностью. Он будет докачан!");
                    }
                    dto.Name = fileName;
                    dto.Data = new byte[bufferSize];

                    while (true)
                    {
                        long size;
                        if ((lenght - curByte) < bufferSize)
                        {
                            size = lenght - curByte;
                        }
                        else
                        {
                            size = bufferSize;
                        }
                        byte[] buffer = new byte[size];
                        stream.Position = curByte;
                        int bytesRead = stream.Read(buffer, 0, (int)size);
                        dto.Data = buffer;
                        if (bytesRead == 0)
                        {
                            break;
                        }

                        client.sendFile(dto);
                        curByte += size;

                        setPos(((int)(stream.Position * 100 / stream.Length)), progressBar);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Процесс передачи файла остановен!\nПри повторной попытке передачи фала вы продолжите с момента остановки!");
            }
            finally
            {
                setDefaultTab1();

                if (stream != null)
                {
                    stream.Close();
                }
                if (curByte == lenght)
                {
                    Filehost.FilehostClient client = new FilehostClient();
                    string index = client.getFileInd(fileName);
                    setInd(index);
                    MessageBox.Show("Файл " + fileNameS + " был успешно отправлен!\nВы можете получить его по ссылке : " + index, "Успешно!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }