locked
Web API and windows form 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

    Wednesday, May 22, 2019 7:13 AM

All replies

  • User61956409 posted

    Hi Stefano,

    Message error: Non-negative number required

    If the ContentLength is negative number, it will cause above issue. In your another thread, I shared the complete code snippet of Web API for clients consume to download file, and you said it seems also work for you.

    [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

    Thursday, May 23, 2019 2:47 AM