【问题标题】:Lost byte when download file下载文件时丢失字节
【发布时间】:2014-04-15 06:48:02
【问题描述】:

我有三个应用程序。
第一:IIS
二:服务(ASP.NET MVC)
第三:客户端(Winform)

文件存储在 IIS 上。
Service public 一个 api 来根据 URL 将文件下载为字节数组。客户端调用Service的api并按扩展名存储文件。


客户端调用服务后,我检查服务,它返回 15500 字节。但是我发现了客户端,它是 13 个字节。


下面是Service上的代码:

[HttpGet]
        public byte[] DownloadData(string serverUrlAddress, string path)
        {
            if (string.IsNullOrWhiteSpace(serverUrlAddress) || string.IsNullOrWhiteSpace(path))
                return null;

        // Create a new WebClient instance
        using (WebClient client = new WebClient())
        {
            // Concatenate the domain with the Web resource filename.
            string url = string.Concat(serverUrlAddress, "/", path);
            if (url.StartsWith("http://") == false)
                url = "http://" + url;

            byte[] data = client.DownloadData(url);
            return data;
        }
    }

下面是客户端的代码:

static void Main(string[] args)
        {
            byte[] data = GetData();
            File.WriteAllBytes(@"E:\a.pdf", data);
        }
    public static byte[] GetData()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:54220/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = client.GetAsync("API/File/DownloadData?serverUrlAddress=www.x.com&path=Data/Folder/file.pdf").Result;
            if (response.IsSuccessStatusCode)
            {
                var yourcustomobjects = response.Content.ReadAsByteArrayAsync().Result;
                return yourcustomobjects;
            }
            else
            {
                return null;
            }

        }
    }

【问题讨论】:

    标签: c# asp.net-mvc asp.net-web-api webclient dotnet-httpclient


    【解决方案1】:

    当返回 CLR 类型时,Web API 会尝试基于 Xml 或 Json 序列化程序序列化对象。这些都不是你想要的。您想要返回原始字节流。试试这个。

        [HttpGet]
        public HttpResponseMessage DownloadData(string serverUrlAddress, string path)
        {
            if (string.IsNullOrWhiteSpace(serverUrlAddress) || string.IsNullOrWhiteSpace(path))
                return null;
    
        // Create a new WebClient instance
        using (WebClient client = new WebClient())
        {
            // Concatenate the domain with the Web resource filename.
            string url = string.Concat(serverUrlAddress, "/", path);
            if (url.StartsWith("http://") == false)
                url = "http://" + url;
    
            byte[] data = client.DownloadData(url);
            return new HttpResponseMessage() { Content = new StreamContent(data) };
        }
    } 
    

    通过返回 HttpResponseMessage,您可以更好地控制 Web API 如何返回响应。默认情况下,StreamContent 会将 Content-Type 标头设置为 application/octet-stream。如果您总是返回 PDF 文件,您可能需要将其更改为“application/pdf”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多