123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Threading.Tasks;
- namespace EasyDevCore.Remote.HttpAccess
- {
- /// <summary>
- /// Represents HTTP content based on a local file. Typically used with PostMultipartAsync for uploading files.
- /// </summary>
- public class FileContent : HttpContent
- {
- /// <summary>
- /// The local file path.
- /// </summary>
- public string Path { get; }
- private readonly int _BufferSize;
- /// <summary>
- /// Initializes a new instance of the <see cref="FileContent" /> class.
- /// </summary>
- /// <param name="path">The local file path.</param>
- /// <param name="bufferSize">The buffer size of the stream upload in bytes. Defaults to 4096.</param>
- public FileContent(string path, int bufferSize = 4096)
- {
- Path = path;
- _BufferSize = bufferSize;
- }
- /// <summary>
- /// Serializes to stream asynchronous.
- /// </summary>
- /// <param name="stream">The stream.</param>
- /// <param name="context">The context.</param>
- /// <returns></returns>
- protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
- {
- using(var fs = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read, _BufferSize, useAsync: true))
- {
- await fs.CopyToAsync(stream, _BufferSize).ConfigureAwait(false);
- }
- }
- /// <summary>
- /// Tries the length of the compute.
- /// </summary>
- /// <param name="length">The length.</param>
- /// <returns></returns>
- protected override bool TryComputeLength(out long length)
- {
- length = -1;
- return false;
- }
- }
- }
|