private static void AddFile(string torrentName, string url) { try { using (HttpClient client = new HttpClient()) { MultipartFormDataContent m = new MultipartFormDataContent(); m.AddFile("torrents", torrentName, "application/x-bittorrent"); HttpResponseMessage response = client.PostAsync(url, m).Result; if (!response.IsSuccessStatusCode) { LOGGER.Warn( $"Tried to download {torrentName} from file to qBitTorrent via {url}. Got following response {response.StatusCode}"); } else { LOGGER.Info( $"Started download of {torrentName} via file to qBitTorrent using {url}. Got following response {response.StatusCode}"); } } } catch (WebException wex) { LOGGER.Warn( $"Could not connect to {wex.Response.ResponseUri} to download {torrentName}, Please check qBitTorrent Settings and ensure qBitTorrent is running with no password required for local connections : {wex.Message}"); } }
public async void MultipartForm_AddFile_Bytes() { var httpClient = GetNewClientFactory().CreateBuilder("sketch7") .WithBaseUrl("http://localhost:5500") .UseLogging(x => x.IsCondensed = true) .Build(); var filePath = "./animal-mustache.jpg"; var fileBytes = File.ReadAllBytes(filePath); var fileName = Path.GetFileName(filePath); var multiForm = new MultipartFormDataContent { { "hero", "Jaina" } }; multiForm.AddFile("file", fileBytes, fileName); var response = await httpClient.CreateRequest("/api/sample/upload") .AsPost() .WithBodyContent(multiForm) .ReturnAsResponse <UploadResult>(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("animal-mustache.jpg", response.Data.FileName); Assert.Equal("image/jpeg", response.Data.ContentType); Assert.Equal(3.96875, response.Data.Size); }
/// <summary> /// Add file from bytes as <see cref="StreamContent"/>. /// </summary> /// <param name="multipartForm"></param> /// <param name="name">Name to use for the http form parameter.</param> /// <param name="fileBytes"></param> /// <param name="fileName"></param> /// <param name="mimeType"></param> public static void AddFile(this MultipartFormDataContent multipartForm, string name, byte[] fileBytes, string fileName, string?mimeType = null) { if (name == null) { throw new ArgumentNullException(nameof(name)); } var memory = new MemoryStream(fileBytes); multipartForm.AddFile(name, memory, fileName, mimeType); }
/// <summary> /// Add file from file stream as <see cref="StreamContent"/>. /// </summary> /// <param name="multipartForm"></param> /// <param name="name">Name to use for the http form parameter.</param> /// <param name="stream">File stream to send.</param> public static void AddFile(this MultipartFormDataContent multipartForm, string name, FileStream stream) { if (name == null) { throw new ArgumentNullException(nameof(name)); } //stream.Position = 0; var fileName = Path.GetFileName(stream.Name); var fileMimeType = MimeTypesMap.GetMimeType(fileName); multipartForm.AddFile(name, stream, fileName, fileMimeType); }
/// <summary> /// Add file as from path as <see cref="StreamContent"/>. /// </summary> /// <param name="multipartForm"></param> /// <param name="name">Name to use for the http form parameter.</param> /// <param name="filePath">Filepath to read from.</param> public static void AddFile(this MultipartFormDataContent multipartForm, string name, string filePath) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } var fileStream = File.OpenRead(filePath); multipartForm.AddFile(name, fileStream); }