我正在研究ASP.NET Core 2.2项目,我需要使用浏览器下载文件,但是当我执行请求时,我只会得到一些Json。 这是我的api端代码以及我在Json中得到的代码。
我有3层代码
我的所有文件都存储在api层中,因此我需要下载客户端。
import React, { Component } from 'react'
import { MyContext } from './MerchantByPromo'
export class MerchantByPromoDetail extends Component {
constructor(props){
super(props)
this.state = {
detailPromo:[],
}
}
UNSAFE_componentWillMount(){
let value = this.context
console.log(value)
}
componentDidMount(){
}
render() {
return (
<MyContext.Consumer>
<p>tes</p>
</MyContext.Consumer>
)
}
}
这是我从上述代码中得到的答复。
public HttpResponseMessage GetFile(string id)
{
string localFilePath = _hostingEnvironment.WebRootPath+"/"+ConfigSettings.UploadPath+Constants.DeliveryFileFolderRelativePath+ "file-201909301642482987.pdf";
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "MyImageFrom";//fileName;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
现在我创建了一个从服务器端到api端的调用,以获取文件结果或字节数组。
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Disposition",
"value": [
"attachment; filename=MyImageFrom"
]
},
{
"key": "Content-Type",
"value": [
"application/pdf"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}