简体   繁体   中英

Sending byte[] representing zip file from one API to another

I am trying to send a zip file I create in one API (lets call this 'API 1') to another (aka 'API 2').

Originally I attempted to just send the byte[] as the response in API 1 but when accessing the response within API 2, the byte[] was a different size and the file was corrupt. I then attempted to convert the byte[] in API 1 to a base64 string and then decode in API 2, but realised the strings were not the same and again, the file was corrupt.

Here is my code as it currently stands...

API 1 Code

[HttpPost]
public IActionResult BuildZipFile(Custom myObject)
{
    CODE HERE WHICH GENERATES ZIP FILE
    byte[] zipFile = System.IO.File.ReadAllBytes(ZIP FILE PATH);
    return Ok(Convert.ToBase64String(zipFile));
}

API 2 Code

[HttpPost]
public IActionResult RetrieveZipFile(Custom myObject)
{
    RestClient client = new RestClient(@"https://localhost:44323/api/ProcessBuild");
    RestRequest request = new RestRequest(Method.POST);
    request.AddHeader("content-type", "application/json");
    request.AddParameter("application/json", JsonConvert.SerializeObject(myObject), ParameterType.RequestBody);
    var response = client.Execute(request);
    byte[] retrievedZipFile = Convert.FromBase64String(response.Content);
    return File(retrievedZipFile, "application/zip", process.Name + ".zip");
}

Currently this returns a zip file which is corrupt. Any help on this would be greatly appreciated!

I am not sure about the mime type. This should work fine. I am not familiar with RestSharp.

using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        return File(System.IO.File.OpenRead(filePath), "application/zip");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM