コード例 #1
0
 public SynchroHandle(SynchroManager.ServerName _server)
 {
     Server      = _server;
     Compression = SynchroManager.ComprLevel.Undefined;
 }
コード例 #2
0
        public static async Task <Boolean> UploadPutFile(Uri _requestUri, StorageFile _file, SynchroManager.ComprLevel _compression)
        {
            // compress to jpeg: 0 (no compression) -> 3 (high)
            if (_compression == SynchroManager.ComprLevel.Medium)
            {
                _file = await Picture.CompressAndSaveFileAsync(_file.Path, ApplicationData.Current.LocalFolder.Path, "Compressed.jpg", 1920, false);
            }
            else if (_compression == SynchroManager.ComprLevel.High)
            {
                _file = await Picture.CompressAndSaveFileAsync(_file.Path, ApplicationData.Current.LocalFolder.Path, "Compressed.jpg", 1024, false);
            }

            if (_file == null)
            {
                return(false);
            }

            Stream _stream = await _file.OpenStreamForReadAsync();

            byte[] buffer;
            long   originalPosition = 0;

            if (_stream.CanSeek)
            {
                originalPosition = _stream.Position;
                _stream.Position = 0;
            }

            try
            {
                byte[] readBuffer = new byte[4096];

                int totalBytesRead = 0;
                int bytesRead;

                while ((bytesRead = _stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;

                    if (totalBytesRead == readBuffer.Length)
                    {
                        int nextByte = _stream.ReadByte();
                        if (nextByte != -1)
                        {
                            byte[] temp = new byte[readBuffer.Length * 2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }
                    }
                }

                buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
            }
            finally
            {
                if (_stream.CanSeek)
                {
                    _stream.Position = originalPosition;
                }
            }

            HttpClient _client = new HttpClient();

            try
            {
                HttpResponseMessage _response = await _client.PutAsync(_requestUri, new ByteArrayContent(buffer));

                return(_response.StatusCode == HttpStatusCode.OK);
            }
            catch (HttpRequestException)
            {
                return(false);
            }
        }