拔掉我的头发。
我通过angular有一个GET请求:
x.DeliverFile = function(fileId) {
$http({
method: "GET",
url: myBase + "Services/DeliverFile?FileID=" + fileId,
headers: myHeaders
}).success(function(data, status, headers, config) {
console.log(data);
});
};
在WEB API中:
If fileinfo.Exists Then
Dim result As New HttpResponseMessage(HttpStatusCode.OK)
With result
.Content = New StreamContent(New FileStream(fileinfo.FullName, FileMode.Open, FileAccess.Read))
.Content.Headers.ContentDisposition = New Headers.ContentDispositionHeaderValue("attachment")
.Content.Headers.ContentDisposition.FileName = fileinfo.Name
.Content.Headers.ContentType = New Headers.MediaTypeHeaderValue("application/octet-stream")
End With
Return result
Else
Return Request.CreateResponse(HttpStatusCode.NotFound)
End If
我已经多次使用httphandlers完成此操作,但显然遗漏了一些关于web api的内容。
我能通过console.log(数据)看到字节;
我无法弄清楚如何让浏览器触发下载。
我在这里阅读了这篇文章:Download file from an ASP.NET Web API method using AngularJS
我无法接受"接受的答案",因为必须有一种方式以跨浏览器的方式触发下载。
答案 0 :(得分:0)
您需要使用类似FileSaver.js的内容,然后才能执行此操作:
x.DeliverFile = function(fileId) {
$http({
method: "GET",
url: myBase + "Services/DeliverFile?FileID=" + fileId,
headers: myHeaders
}).success(function(data, status, headers, config) {
var blob = new Blob(data, {type: "text/plain;charset=utf-8"});
saveAs(blob, "fileName.txt");
});
};