示例#1
0
        public async Task <Tuple <bool, string> > GetFile(string fileName, string fileExt)
        {
            FileReq req = new FileReq
            {
                Fullfilename = fileName + fileExt
            };

            string fileNameLocal = "c:\\temp\\" + fileName + "_"
                                   + System.Guid.NewGuid().ToString() + fileExt;

            if (destinationStream == null)
            {
                //create file to save stream to
                destinationStream = File.Create(fileNameLocal);
            }

            var stream = new CodedOutputStream(destinationStream, true);

            using (var call = _client.GetFile(req))
            {
                while (await call.ResponseStream.MoveNext())
                {
                    var arr = call.ResponseStream.Current.PayLoad.ToByteArray();
                    destinationStream.Write(arr, 0, arr.Length);
                }
            }
            await destinationStream.FlushAsync();

            stream.Flush();
            stream.Dispose();

            if (destinationStream != null)
            {
                destinationStream.Dispose();
                destinationStream.Close();
                destinationStream = null;
            }
            return(new Tuple <bool, string>(true, fileNameLocal));
        }
示例#2
0
        public override async Task GetFile(FileReq request, IServerStreamWriter <Chunk> responseStream, ServerCallContext context)
        {
            long dataToRead;

            byte[] buffer = new Byte[4096];
            int    length;
            int    bytesRead = 0;
            Chunk  msg       = new Chunk();

            try
            {
                Stream inpStream = new FileStream("c:\\temp\\" + request.Fullfilename, FileMode.Open,
                                                  FileAccess.Read, FileShare.Read);
                dataToRead = inpStream.Length;
                int start = 0;
                inpStream.Seek(start, SeekOrigin.Begin);
                while (dataToRead > 0)
                {
                    // Read the data in buffer
                    length = inpStream.Read(buffer, 0, buffer.Length);

                    // Write the data to the current output stream
                    msg.PayLoad = ByteString.CopyFrom(buffer, 0, buffer.Length);
                    await responseStream.WriteAsync(msg);

                    bytesRead += buffer.Length;

                    buffer     = new Byte[buffer.Length];
                    dataToRead = dataToRead - buffer.Length;
                }
            }
            catch (Exception ex)
            {
                string str = ex.ToString();
            }
        }