0

我想从网络服务下载文件。我试过这样做,但我无法在高级休息客户端中测试它。这是我的代码

public static HttpResponseMessage FileAsAttachment(string path, string filename)
        {
            if (File.Exists(path))
            {

                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                var stream = new FileStream(path, FileMode.Open);
                result.Content = new StreamContent(stream);
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                result.Content.Headers.ContentDisposition.FileName = filename;
                return result;
            }
            return new HttpResponseMessage(HttpStatusCode.NotFound);
        }

我总是有响应 " "$id": "1" "Message": "No HTTP resource was found that match the request URI ' http://localhost:36690/api/Data/FileAsAttachment?path=F :\&filename= claim.jpg'。" "MessageDetail": "在控制器 'Data' 上找不到与名称 'FileAsAttachment' 匹配的操作。" 我的控制器名称是 data

4

1 回答 1

1

Action method不应该是静态的。当我static从您的代码中删除它时,它正在工作。

为什么需要将其声明为静态方法?

请查看以下链接。

链接 1 链接2

更新:评论后,尝试装饰方法[HttpGet]

 [HttpGet]
 public HttpResponseMessage FileAsAttachment(string path, string filename)
于 2016-02-15T14:20:10.873 回答