Esempio n. 1
0
        public bool PostFile(string fileName, string address, FileArgs args = null)
        {
            FileStream fs = null;
            long       bytesRemaining;

            try
            {
                if (args != null)
                {
                    args.Identity = identity;
                }
                else
                {
                    args = new FileArgs()
                    {
                        Identity = identity
                    }
                };
                string path = Path.Combine(fileName);
                fs             = File.OpenRead(path);
                bytesRemaining = fs.Length;
                EndpointAddress baseAddress = new EndpointAddress(address + MessageReceiver.Endpoint);
                var             factory1    = new ChannelFactory <IMessageReceiver>(new WSHttpBinding(), baseAddress);
                using (IMessageReceiver chanl = factory1.CreateChannel())
                {
                    chanl.OpenFileForWrite(Path.GetFileName(fileName), args);
                    while (true)
                    {
                        long   bytesToRead  = Math.Min(1024, bytesRemaining);
                        byte[] blk          = new byte[bytesToRead];
                        long   numBytesRead = fs.Read(blk, 0, (int)bytesToRead);
                        bytesRemaining -= numBytesRead;

                        chanl.WriteFileBlock(blk);
                        if (bytesRemaining <= 0)
                        {
                            break;
                        }
                    }
                    chanl.CloseFile();
                }
                fs.Close();
            }
            catch (Exception ex)
            {
                lastError = ex.Message;
                Console.WriteLine("Reason for bombout : " + ex.Message);
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        public bool OpenFileForWrite(string name, FileArgs args = null)
        {
            int retry = 5, tries = 0;

            while (tries < retry)
            {
                try
                {
                    FileRequestReceived?.Invoke(name, args);
                    dir = baseDirectory;
                    string writePath = Path.Combine(dir, name);
                    fs = File.OpenWrite(writePath);
                    return(fs.CanWrite);
                }
                catch (Exception ex)
                {
                    lastError = ex.Message;
                    tries++;
                    Thread.Sleep(1000);
                }
            }
            return(false);
        }
Esempio n. 3
0
 /// <summary>
 /// Upload file to the address
 /// </summary>
 /// <param name="filename"></param>
 /// <returns></returns>
 public bool PostFile(string filename, string Addr, FileArgs args = null)
 {
     return(sender.PostFile(filename, Addr, args));
 }