dimanche 25 août 2019

Get a file from public Asp Core API which consumes DMZ API

I have two APIs: DMZ and Public (public consumes dmz) 1. DMZ:

    public FileStreamResult GetContent(int id)
    {
        var content = from m in db.messagestoimages
                      where m.Message == id
                      select m;

        if (content == null || content.Count() == 0)
        {
            return null;
        }
        string fileName = content.First().ImageURL;

        string fullPath = AppDomain.CurrentDomain.BaseDirectory  + fileName;
        if (File.Exists(fullPath))
        {
            var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            var result = new FileStreamResult(fileStream, "image/jpeg");
            return result;
        }

        return null;
    }

2: Public API

   [HttpGet("{id}")]
    public FileStreamResult Get(string id)
    {
        try
        {
            {
                using (var client = new HttpClient())
                {
                    try
                    {
                        string host = configuration.GetSection("MySettings").GetSection("OctopusURL").Value;

                        client.BaseAddress = new Uri(host);

                        var response = client.GetAsync("api/Content/" + id);
                        var stream = response.Result.Content.ReadAsStreamAsync().Result;
                        return new FileStreamResult(stream, "image/jpeg")
                        {
                            FileDownloadName = "test.jpeg"
                        };
                    }
                    catch (Exception ex)
                    {
                        return null;
                    }
                }
            }

        }
        catch (Exception ex)
        {
            return null; // 500 is generic server error
        }
    }

My problem is when i get File from public API, it is wrong, i can not open the file, windows says format is not supported. Size of the file that i get is smaller than original which probably means that file was transferred partially or with error.

enter image description here




Aucun commentaire:

Enregistrer un commentaire