使用web-api获取和归档内容并使用它

问题描述 投票:0回答:1

我正在尝试阅读PDF文件,使用httpget web api发送其内容(字节数组),然后在客户端中,根据来自Web API的get数据创建一个新的PDF文件。但是 - 当传输Byte []数据时 - 它正在被破坏,这意味着数据的大小会发生变化。这是我的代码:这是web api get方法:

[Route("api/SystemSigner/Files/GetFile")]
public Byte[] GetFile()
{
    var path = @"<pdf path>.pdf";
    var dataBytes = File.ReadAllBytes(path);
    return dataBytes;
}

这是用于消费它的web api“communicator”(它返回一个不同大小的任务):

public static async Task<Byte[]> GetFile()
{
    Byte[] retrunedTask = null;
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(ConfigurationManager.AppSettings["WebApiUri"] + "/Files/GetFile");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/pdf"));
        retrunedTask = await client.GetByteArrayAsync(client.BaseAddress);
    }
    return retrunedTask;
}

这是客户消费者:

private async void CreateFile()
{
    Byte[] fileByteArray = await WebApiCommunicator.GetFile();
    string filePath = @"<File path to save the file>";
    File.WriteAllBytes(filePath , fileByteArray);
}
c# arrays pdf asp.net-web-api file-get-contents
1个回答
0
投票

以下是我对此问题的解决方案:如何在web-api中正确获取文件内容:这是web-api get方法

  [Route("api/<Controller name>/Files/GetFile")]
    public HttpResponseMessage GetFile(string fileName)
    {
        string filePath = <Path to the wanted file on the server - same file system >
        if (!File.Exists(filePath))
        {
            return new HttpResponseMessage(HttpStatusCode.NotFound);
        }
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
        response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(filePath);
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        return response;
    }

这是来自客户端的消费代码:

 internal async static Task<Byte[]> GetFile(string fileName) 
{
        Byte[] returnedTask = null;
        using (var client = new HttpClient())
        {
            UriBuilder uriBuilder = new UriBuilder(string.Format(@"{0}{1}", <Web api URI>, "/Files/GetFile"));
            if (!string.IsNullOrEmpty(fileName))
            {
                uriBuilder.Query = string.Format("fileName={0}", fileName);
            }
            client.BaseAddress = new Uri(uriBuilder.ToString());
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/pdf"));
            returnedTask = await client.GetByteArrayAsync(client.BaseAddress);
        }
        return returnedTask;
    }

这是客户端上使用相同内容的服务,创建和保存文件的代码:

Byte[] fileByteArray = await <class name (static)>.GetFile(fileName);
        File.WriteAllBytes(<Local Path to save the file>, fileByteArray);
© www.soinside.com 2019 - 2024. All rights reserved.