0

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.

Jacqques
  • 31
  • 4
  • This is wrong : "I am trying to send a file on my pc to my client". You application is a web application where the client sends a request to the server, and the server send back a response to client. You have a controller which does following 1) Client sends Request to server using PUT 2) Server receives request using GET 3) Server processes request 4) Server sends response with PUT 5) Client receives the response with GET. It is not clear what your code is doing and where it is failing. – jdweng Jan 26 '22 at 14:26
  • I am not sure I follow. I have a web application with a controller that accepts a get request and responds. The response should be the file. How do I make it so the response is the file? Why do you say my client sends a put request? HttpClient.GetStreamAsync(uri) should be a get request no? How else would I call the web application? – Jacqques Jan 26 '22 at 15:09
  • Web app has client code and server code.Client and server can be in the same virtual application. A connection is made between client and server.You can have a controllers in either or both the client and server and then you have two controllers. The PUT is optional at both client and server. It is really the body of the request/response. Best way of sending a file is make it a mime attachment to the message. A mime starts with two dashes on a new line. See : https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140)?force_isolation=true – jdweng Jan 26 '22 at 15:20
  • The `100 Continue` seems strange to me. Can you try `httpClient.DefaultRequestHeaders.ExpectContinue = false;` on the client side? – Gene Jan 26 '22 at 18:26

1 Answers1

1

[HttpGet("ftplog")]
public FileStreamResult Get()
{
   string fileName = "FileZilla Server.log";
   string path = @"C:\Users\jacqq\source\repos\testing\testing\Views\Home\FileZilla Server.log";
   var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
   var memoryStream = new MemoryStream();       
   fileStream.SaveAs(memoryStream);
   memoryStream.Position = 0;
   return new FileStreamResult(memoryStream, "application/octet-stream")
   {
       FileDownloadName = $"{fileName}.log"
   };
}