在Angularjs和WebApi中下载Excel文件

这成功地下载文件,但是当我打开文件excel抱怨说,它已损坏,并没有保存在适当的文件格式。 我也尝试使用FIleSaver.js,但也没有运气。 同样的错误。 对于如何解决这个问题,有任何的build议吗?

服务器代码:

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read); MemoryStream ms = new MemoryStream(); fs.CopyTo(ms); response.Content = new ByteArrayContent(ms.ToArray()); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileInfo.Name }; return response 

客户端(AngularJS服务代码):

 var req = { url: fpReportsWebAPIURL + 'api/Export/DownloadFile', method: 'POST', responseType: 'arraybuffer', //data: json, //this is your json data string headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' } }; return $http(req).then(function (data) { var type = data.headers('Content-Type'); var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); saveAs(blob, 'helloAgain' + '.xlsx'); return true; });