I have to write the promise call and component method to call the webAPI that returns csv file. I can see, my code wont work without creating a proper header. Please let me know, how I can fix this issue
Component:
downloadfile() { this.isLoading = true; this.customerMonitoringService.getMyFileFromBackend(this.productionDataUrl).subscribe( res => { this.isLoading = false; res; }, (error: any) => Observable.throw(error || 'Server error') ); }
cust-monitoring.service.ts
getMyFileFromBackend(productionURL: string): Observable {
return this.http.get("`"+productionURL+"`")
.map(res => res.text())
.catch((error: any) => Observable.throw(error || 'Server error'));
}
Web API call
[HttpGet]
[HttpPost]
public async Task<IHttpActionResult> Index(string systemName)
{
var negotiator = Configuration.Services.GetContentNegotiator();
var mediaTypeFormatterCollection = new MediaTypeFormatterCollection();
mediaTypeFormatterCollection.Clear();
mediaTypeFormatterCollection.Add(new ProductionPeriodFormatter());
mediaTypeFormatterCollection.Add(new JsonMediaTypeFormatter());
var result = negotiator.Negotiate(typeof(ProductionPeriod), Request, mediaTypeFormatterCollection);
if (result == null)
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable));
var production = await Production(systemName);
var content = new ObjectContent<ProductionPeriod>(
production,
result.Formatter,
result.MediaType.MediaType
);
if (result.Formatter is JsonMediaTypeFormatter)
{
((JsonMediaTypeFormatter) result.Formatter).SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
content.Value = new ProductionPeriod(production.Start, production.End, production.TimeZone, interval.Value,
NormalizedIntervals(production, interval.Value));
}
else
{
try
{
WebRequest.DefaultWebProxy = null;
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "production.csv"
};
}
catch (Exception ex)
{
string msg = "";
msg = ex.Message.ToString();
}
}
var response = new HttpResponseMessage()
{
Content = content
};
return ResponseMessage(response);
}
Aucun commentaire:
Enregistrer un commentaire