这是我有时使用它的方式。我假设您将附件作为服务器响应提供。
我从REST服务中这样设置响应头
response.setHeader("Content-Disposition","attachment;filename=XYZ.csv");
function(response, status, xhr){ var filename = ""; var disposition = xhr.getResponseHeader('Content-Disposition'); if (disposition && disposition.indexOf('attachment') !== -1) { var filenameRegex = /filename[^;=n]*=((['"]).*?2|[^;n]*)/; var matches = filenameRegex.exec(disposition); if (matches != null && matches[1]) {filename = matches[1].replace(/['"]/g, ''); } }}
编辑:编辑答案以适合您的问题-使用单词
inline代替
attachment
function(response, status, xhr){ var filename = ""; var disposition = xhr.getResponseHeader('Content-Disposition'); if (disposition && disposition.indexOf('inline') !== -1) { var filenameRegex = /filename[^;=n]*=((['"]).*?2|[^;n]*)/; var matches = filenameRegex.exec(disposition); if (matches != null && matches[1]) {filename = matches[1].replace(/['"]/g, ''); } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)