0

从我的 API Web 服务下载文件时遇到问题。当我在高级休息客户端测试它时。我没有得到我的文件,但我得到了字符。这是我的http://41.128.183.109:1212/api/Data/get filename = claim.jpg

我的网络服务的屏幕截图: 在此处输入图像描述 这是我的回复: 在此处输入图像描述

这是我的代码:

 public HttpResponseMessage get ([FromUri]string filename)
{
    string path = HttpContext.Current.Server.MapPath("~/TransientStorage/" + filename);
    if (!File.Exists(path))
    {
        throw new HttpResponseException( HttpStatusCode.NotFound);
    }

    try
    {
        MemoryStream responseStream = new MemoryStream();
        Stream fileStream = File.Open(path, FileMode.Open);
        bool fullContent = true;
        if (this.Request.Headers.Range != null)
        {
            fullContent = false;

            // Currently we only support a single range.
            RangeItemHeaderValue range = this.Request.Headers.Range.Ranges.First();


            // From specified, so seek to the requested position.
            if (range.From != null)
            {
                fileStream.Seek(range.From.Value, SeekOrigin.Begin);

                // In this case, actually the complete file will be returned.
                if (range.From == 0 && (range.To == null || range.To >= fileStream.Length))
                {
                    fileStream.CopyTo(responseStream);
                    fullContent = true;
                }
            }
            if (range.To != null)
            {
                // 10-20, return the range.
                if (range.From != null)
                {
                    long? rangeLength = range.To - range.From;
                    int length = (int)Math.Min(rangeLength.Value, fileStream.Length - range.From.Value);
                    byte[] buffer = new byte[length];
                    fileStream.Read(buffer, 0, length);
                    responseStream.Write(buffer, 0, length);
                }
                // -20, return the bytes from beginning to the specified value.
                else
                {
                    int length = (int)Math.Min(range.To.Value, fileStream.Length);
                    byte[] buffer = new byte[length];
                    fileStream.Read(buffer, 0, length);
                    responseStream.Write(buffer, 0, length);
                }
            }
            // No Range.To
            else
            {
                // 10-, return from the specified value to the end of file.
                if (range.From != null)
                {
                    if (range.From < fileStream.Length)
                    {
                        int length = (int)(fileStream.Length - range.From.Value);
                        byte[] buffer = new byte[length];
                        fileStream.Read(buffer, 0, length);
                        responseStream.Write(buffer, 0, length);
                    }
                }
            }
        }
        // No Range header. Return the complete file.
        else
        {
            fileStream.CopyTo(responseStream);
        }
        fileStream.Close();
        responseStream.Position = 0;

        HttpResponseMessage response = new HttpResponseMessage();
        response.StatusCode = fullContent ? HttpStatusCode.OK : HttpStatusCode.PartialContent;
        response.Content = new StreamContent(responseStream);
        return response;
    }
    catch (IOException)
    {
        throw new HttpResponseException( HttpStatusCode.InternalServerError);

    }

}
4

2 回答 2

0

在响应对象中添加内容类型。

 HttpResponseMessage response = new HttpResponseMessage();
        response.StatusCode = fullContent ? HttpStatusCode.OK : HttpStatusCode.PartialContent;
        response.Content = new ByteArrayContent(responseStream.ToArray());
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
        return response;
于 2016-02-17T11:35:19.827 回答
0

我想你确实得到了你的文件。这就是 jpg 文件的样子(至少标题看起来不错)。不过,它是字节格式,而不是呈现为图像。尝试将其作为 *.jpg 文件存储在硬盘上或使用 img 标签显示。

于 2016-02-17T11:22:30.523 回答