I am trying to send a file on my pc to my client. I have looked around and most answers resembles this: How to return a file (FileContentResult) in ASP.NET WebAPI
So I have tried implementing it myself, but I only manage to receive the headers. I must assume I am doing something wrong somewhere, but I can't tell where. I have a little trouble telling the c# versions apart, but the target it should work on is .Net 4.8.
My code for sending the file:
[HttpGet("ftplog")]
public HttpResponseMessage Get()
{
String fileName = "FileZilla Server.log";
String path = @"C:\Users\jacqq\source\repos\testing\testing\Views\Home\FileZilla Server.log";
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
var memoeryStream = new MemoryStream();
fileStream.CopyTo(memoeryStream);
memoeryStream.Seek(0, SeekOrigin.Begin);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(memoeryStream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileName.ToString()
};
return result;
}
I have tried sending the FileStream instead of a MemoryStream and I have tried sending the file as ByteArrayContent, but I always only receive headers. I have checked, and the file is read correctly with data. It might be that my recieving code is wrong?
public static async Task DownloadFileTaskAsync(HttpClient client, Uri uri, string FileName)
{
var s = await client.GetStreamAsync(uri);
StreamReader reader = new StreamReader(s) ;
Console.WriteLine("Starting");
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("stopping");
}
I am new to c#, so it might be I have overlooked something. If anyone know of a different/better way to send a file, I would be keen to hear that too. Here is what I recieve:
{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Type","value":["application/octet-stream"]},{"key":"Content-Disposition","value":["attachment; filename=\"FileZilla Server.log\""]}]},"statusCode":100,"reasonPhrase":"Continue","headers":[],"trailingHeaders":[],"requestMessage":null,"isSuccessStatusCode":false}
Please help, have been stuck for little too long.