[英]Sending files to the browser with AngularJS, WebAPI, not triggering download?
拔掉我的頭發。
我通過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(data)看到字節;
我不知道如何讓瀏覽器觸發下載。
我在這里閱讀了這篇文章: 使用AngularJS從ASP.NET Web API方法下載文件
我在接受“接受的答案”時遇到了麻煩,因為必須有一種以跨瀏覽器兼容的方式觸發下載的方法。
您需要使用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");
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.