将响应另存为文件
Save response as file
>我有WebAPI方法,该方法将HttpResponseMessage
文件作为内容返回.csv
:
private static HttpResponseMessage FileAsAttachment(string file)
{
var now = DateTime.Now;
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(file);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //attachment will force download
result.Content.Headers.ContentDisposition.FileName = string.Format("Report-{0}.csv", now.ToString("MMMM"));
return result;
}
所以我只有点击函数,它调用服务器:
$scope.GenerateReport = function() {
var endDate = '2016-04-30';
UserDaysSummary.generateReport({endDate: endDate }, function (result) {
console.log("Export");
});
}
但我所拥有的只是包含数据的响应。我试图使用这个和这个答案将其作为文件获取,但这并没有改变任何东西。
最好,对服务器的调用具有GET
方法,顺便说一句
你的GenerateReport
函数是否返回一个承诺? 试试这个:
userDaysSummary.generateReport = function(endDate) {
var defer = $q.defer();
$http.get('path/to/api', { endDate: endDate }, { responseType: 'arrayBuffer' }).then(function(data, status, headers, config) {
var results = {
data: data, //your file is here
headers: headers(), //headers are here
status: status,
config: config
};
//return a success promise containing the response object
defer.resolve(results);
}, function(data, status, headers, config) {
defer.reject(data);
});
return defer.promise;
}
然后,使用承诺下载文件:
userDaysSummary.generateReport(endDate).then(function(response) {
//get the file
var octetStreamMime = 'application/octet-stream';
//get the headers' content disposition
var cd = response.headers["content-disposition"];
//get the file name with regex
var regex = /filename[^;='n]*=((['"]).*?'2|[^;'n]*)/;
var match = regex.exec(cd);
//is there a fiel name?
var fileName = match[1] || "myDefaultFileName.csv";
//replace leading and trailing slashes that C# added to your file name
fileName = fileName.replace(/'"/g, "");
//determine the content type from the header or default to octect stream
var contentType = response.headers["content-type"] || octetStreamMime;
//finally, download it
try {
var blob = new Blob([response.data], {type: contentType});
//downloading the file depends on the browser
//IE handles it differently than chrome/webkit
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}
} catch (exc) {
console.log("Save Blob method failed with the following exception.");
console.log(exc);
}
}, function(error) {
//an error occurred while trying the API, handle this
});
相关文章:
- 如何将 html5 画布另存为窗口 8 Metro 应用程序中的图像文件
- 更改音频速度,然后另存为新文件
- 将响应另存为文件
- 使用JavaScript,我可以上传一个word文件并使用.replaces然后另存为新文档吗
- 将文件另存为.pdf或.doc
- 在IE8中使用“另存为”execCommand以ANSI格式而不是UCS2-Little Endian保存XML文件
- 将编辑的 SVG 文件另存为新文件或保存到数据库
- 使用 JavaScript 打开“另存为”/“下载”对话框以下载动态创建的文件
- 如何在 C# .net 中将.aspx页另存为 html 文件时绘制动态 JQ 图
- 将音频 blob 另存为 rails 应用中的文件
- 禁用保存并在下载文件 ASP.NET 另存为
- 如何使用javascript将默认文件扩展名放在另存为对话框中
- 节点 - 将 m3u8 流另存为 mp3 文件
- 下载文本文件,并在nodejs中另存为字符串
- “另存为blob”需要很长时间才能从给定的url下载文件
- 另存为文件的一部分
- 如何通过提示“另存为”对话框从网页中保存文件
- 如何在单击'时设置文件/图像的名称;将图像另存为'
- 脚本不起作用,要么放在html上,要么另存为.js文件
- 如何禁用浏览器另存为文件->另存为)