I'm attempting to move pdf files between two web services. To start, I successfully downloaded a file form the source service to local storage using await httpClient.GetStreamAsync(varPdfUrl) and then uploaded it to the destination service using File.OpenRead() which is commented out below. When I attempt to use await httpClient.GetStreamAsync(varPdfUrl) to load the StreamContent directly I get the following error.
Error Message: System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..
var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, uploadResponse.url);
var content = new MultipartFormDataContent();
// Populate MultipartFormDataContent
content.Add(new StringContent(uploadResponse.fields.key), "key");
content.Add(new StringContent(uploadResponse.fields.ContentType), "Content-Type");
// Add File Stream
string fileName = drawingTemp.ProjectNumber + "-" + drawingTemp.Title + ".pdf";
//string localPath = @"C:\... \CMP\Drawing Downloads\";
//var fileSteamLocalFile = new StreamContent(File.OpenRead(localPath + fileName));
//content.Add(fileSteamLocalFile, "file", fileName);
var fileSteam = new StreamContent(await httpClient.GetStreamAsync(drawingTemp.PdfUrl));
content.Add(fileSteam, "file", fileName );
content.Add(new StringContent(uploadResponse.uuid), "uuid");
try
{
//Upload File to AWS
request.Content = content;
var responseUploadFileMsg = await httpClient.SendAsync(request);
}
I guess all StreamContent objects are not created equal... please advise. Thanks!
UPDATE
I'm still unable to populate StreamContent with httpClient.GetStreamAsync() but as a work around I'm able to write the file to a project folder "wwwroot/Files" and then use File.OpenRead() to read the file and populate StreamContent. I would still appreciate any ideas as to why populating StreamContent directly with httpClient.GetStreamAsync() throws the error listed above.
var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, uploadResponse.url);
var content = new MultipartFormDataContent();
// Add File Stream
string fileName = drawingTemp.ProjectNumber + "-" + drawingTemp.Title + ".pdf";
Stream fileSteam = await httpClient.GetStreamAsync(drawingTemp.PdfUrl);
// Convert Stream to File and save temporarily to wwwroot/Files
string writeFullPath = @"wwwroot\Files\" + fileName;
if (!File.Exists(writeFullPath))
{
using var fileLocalPath = File.Create(writeFullPath);
{
fileSteam.CopyTo(fileLocalPath);
fileSteam.Dispose();
}
}
string readFullPath = Environment.CurrentDirectory + @"\wwwroot\Files\" + fileName;
// Add File Stream to Content
var fileSteamLocalFile = new StreamContent(File.OpenRead(readFullPath));
content.Add(fileSteamLocalFile, "file", fileName);
content.Add(new StringContent(uploadResponse.uuid), "uuid");
try
{
//Upload File to AWS
request.Content = content;
var responseUploadFileMsg = await httpClient.SendAsync(request);
fileSteamLocalFile.Dispose();
// Delete temp file
if (File.Exists(readFullPath))
{
File.Delete(readFullPath);
}