在React

时间:2018-08-10 14:25:10

标签: c# reactjs api

我将这样返回文件thorugh api:

[HttpGet]
[Authorise()]
[Route("{guidanceType}", Name = Routes.Api.Guidances.View_GET)]
public HttpResponseMessage GetGuidance(string guidanceType)
{
    if (guidanceType == "guidance")
    {
        var path = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Guidances/" + FileName);
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(path);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        result.Content.Headers.ContentLength = stream.Length;
        return result;
    }
    return new HttpResponseMessage(HttpStatusCode.NotFound);
}

具有此api方法:

class GuidanceApi {

  getGuidance (guidanceType:string) {
    return `${interpolateUri(basePath, {'guidanceType' : guidanceType})}`;
  }
}

并将其保存在我的html中:

<a href={GuidanceApi.getGuidance('guidance')} className={`btn btn-info pull-right`}>Why use Connectors?</a>

但是,与其下载文件,不如给我做这件事:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Content-Disposition: attachment; filename=web_Version_1.pdf Content-Type: application/pdf Content-Length: 137609 } 

似乎文件已返回,但是下载文件的方式是什么?

0 个答案:

没有答案