WebApi 无法将正确的编码文件发送到前端 (angularjs)
WebApi impossible to send proper encoded file to front (angularjs)
我收到一个 Base64 编码的字符串,其中包含一个 word 文档(由 thunderHead 生成),我用以下方法解码:
byte[] dataBytes = Convert.FromBase64String(data)
当我直接将这个 byteArray 写入文件时:
File.WriteAllBytes("myFile.doc", dataByte);
word文档写对了(é,è都显示对了),但是我把这个byteArray放到最前面就不行了(angularjs):
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(dataByte);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msword");(also tried "application/octet-stream")
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachement");
response.Content.Headers.ContentDisposition.Filename = "toto.doc"
return response;
我想不通....请提供帮助,谢谢!
解决了我的问题,正如@peco 指出的那样,问题出在前面,我正在做一个 $http.post(url, data).then ... 并且错过了添加{responseType: 'arrayBuffer'} 作为第三个参数:$http.post(url, data, {responseType: 'arrayBuffer'}).then... 谢谢.
我收到一个 Base64 编码的字符串,其中包含一个 word 文档(由 thunderHead 生成),我用以下方法解码:
byte[] dataBytes = Convert.FromBase64String(data)
当我直接将这个 byteArray 写入文件时:
File.WriteAllBytes("myFile.doc", dataByte);
word文档写对了(é,è都显示对了),但是我把这个byteArray放到最前面就不行了(angularjs):
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(dataByte);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msword");(also tried "application/octet-stream")
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachement");
response.Content.Headers.ContentDisposition.Filename = "toto.doc"
return response;
我想不通....请提供帮助,谢谢!
解决了我的问题,正如@peco 指出的那样,问题出在前面,我正在做一个 $http.post(url, data).then ... 并且错过了添加{responseType: 'arrayBuffer'} 作为第三个参数:$http.post(url, data, {responseType: 'arrayBuffer'}).then... 谢谢.