locked
Windows form and WEB API RRS feed

  • Question

  • User-941767363 posted

    Hello,

    I'm developing a windows form application (c#) that calls WEB API services (developed by me) and in particular I'm trying to "downloading" an Excel file (report) that it was previously created in a server folder ("Stampe").

    I'm in trouble to "manage" client side, the response (HttpResponseMessage) from WEB API service.

    • SERVER side:

       [HttpGet]
            public HttpResponseMessage GetFile(string FilePath, string FileName)
            {
                return GetFileStampa(FilePath, FileName);
            }

            public HttpResponseMessage GetFileStampa(string FilePath, string fileName)
            {
                var path = Path.Combine(FilePath, fileName);

                var response = new HttpResponseMessage(HttpStatusCode.OK);

                var stream = new FileStream(path, FileMode.Open);
                response.Content = new StreamContent(stream);

                response.Content.Headers.ContentType =
                  new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition =
                  new ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = fileName;

                return response;
            }

    • CLIENT side (windows form):

    string FilePath = @"........\Stampe\";
                string FileName = @"Test.xlsx";

                string UrlGetFile = Url + "/File?" + "FilePath=" + FilePath + "&FileName=" + FileName;
                Uri UriGetFile = new Uri(UrlGetFile);
                HttpWebRequest request = WebRequest.Create(UriGetFile) as HttpWebRequest;
                request.Method = "GET";
                request.ContentType = "application/octet-stream"; //"text/xml";
                string results = string.Empty;
                HttpWebResponse response;
                using (response = request.GetResponse() as HttpWebResponse)
                {
                    Stream objStream = response.GetResponseStream();
                    BinaryReader breader = new BinaryReader(objStream);
                    byte[] data = breader.ReadBytes((int)response.ContentLength);
                    File.WriteAllBytes("d:\\temp\\MyTest.xlsx", data);
                }

    the instruction "response.ContentLength" is always to -1. Message error: Non-negative number required

    Where am I doing wrong (client or server side)?

    Best

    Stefano

    Tuesday, May 21, 2019 4:23 PM

Answers

  • User61956409 posted

    Hi Stefano,

    I changed Web APi service according your suggestion and now everything is fine, I receive the Excel file (client side).

    Thank you so much, you solved my problem

    Glad to hear that the code snippet help you solved the problem. You can mark it as answer, which would help other community members quickly find this thread and answer to solve similar problems. Thanks.

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2019 2:52 AM

All replies

  • User1120430333 posted

    You should post to the WebAPI forum or request that this thread be moved to the WebAPI forum for a more quick response.

    Tuesday, May 21, 2019 11:03 PM
  • User-941767363 posted

    ok, thank you

    Wednesday, May 22, 2019 7:12 AM
  • User61956409 posted

    Hi Mila,

    the instruction "response.ContentLength" is always to -1. Message error: Non-negative number required

    If you debug the code, you would find the ContentLength might be -1, which cause the issue.

    According to your description, it seems that you'd like to expose Web API for clients (windows form application) consume to download file, to achieve the requirement, you can try to modify the API action like below to return File directly.

    [HttpGet]
    public ActionResult GetFile(string FilePath, string FileName)
    {
        return GetFileStampa(FilePath, FileName);
    }
    
    public ActionResult GetFileStampa(string FilePath, string fileName)
    {
        var path = Path.Combine(FilePath, fileName);
    
        var response = new HttpResponseMessage(HttpStatusCode.OK);
    
        byte[] bytes = System.IO.File.ReadAllBytes(path);
    
    
        return File(bytes, "application/octet-stream");
    }

    With Regards,

    Fei Han

    Wednesday, May 22, 2019 7:41 AM
  • User-941767363 posted

    Hello Fei,

    I changed Web APi service according your suggestion and now everything is fine, I receive the Excel file (client side).

    Thank you so much, you solved my problem

    Best

    Stefano

    Wednesday, May 22, 2019 10:38 AM
  • User61956409 posted

    Hi Stefano,

    I changed Web APi service according your suggestion and now everything is fine, I receive the Excel file (client side).

    Thank you so much, you solved my problem

    Glad to hear that the code snippet help you solved the problem. You can mark it as answer, which would help other community members quickly find this thread and answer to solve similar problems. Thanks.

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2019 2:52 AM