非表示のテキストエリアにテキストがあります。ボタンがクリックされたときに、テキストを.txtファイルとしてダウンロードできるようにしたいと考えています。AngularJSまたはJavascriptを使用してこれは可能ですか?
非表示のテキストエリアにテキストがあります。ボタンがクリックされたときに、テキストを.txtファイルとしてダウンロードできるようにしたいと考えています。AngularJSまたはJavascriptを使用してこれは可能ですか?
回答:
を使用して、このようなことを行うことができますBlob。
<a download="content.txt" ng-href="{{ url }}">download</a>
あなたのコントローラーで:
var content = 'file content for example';
var blob = new Blob([ content ], { type : 'text/plain' });
$scope.url = (window.URL || window.webkitURL).createObjectURL( blob );
URLを有効にするため:
app = angular.module(...);
app.config(['$compileProvider',
function ($compileProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|blob):/);
}]);
その点に注意してください
createObjectURL()を呼び出すたびに、同じオブジェクトのURLがすでに作成されている場合でも、新しいオブジェクトURLが作成されます。これらは、不要になったときにURL.revokeObjectURL()を呼び出して解放する必要があります。ドキュメントがアンロードされると、ブラウザはこれらを自動的に解放します。ただし、最適なパフォーマンスとメモリ使用量を得るために、明示的にアンロードできる安全な時間があれば、そうする必要があります。
出典:MDN
$scope.urlうまくいきませんでした。window.location代わりに使用しなければなりませんでした。
download属性は、IEやSafariのバージョンでサポートされていないもののcaniuse.com/#feat=download
ボタンをクリックするだけで、次のコードを使用してダウンロードできます。
HTMLで
<a class="btn" ng-click="saveJSON()" ng-href="{{ url }}">Export to JSON</a>
コントローラ内
$scope.saveJSON = function () {
$scope.toJSON = '';
$scope.toJSON = angular.toJson($scope.data);
var blob = new Blob([$scope.toJSON], { type:"application/json;charset=utf-8;" });
var downloadLink = angular.element('<a></a>');
downloadLink.attr('href',window.URL.createObjectURL(blob));
downloadLink.attr('download', 'fileName.json');
downloadLink[0].click();
};
$http.get(...)設定してくださいをresponseType:'arraybuffer':ここで説明するようにstackoverflow.com/questions/21628378/...
これを試して
<a target="_self" href="mysite.com/uploads/ahlem.pdf" download="foo.pdf">
このサイトにアクセスしてください:)
downloadIEやSafariのいずれのバージョンでもまだサポートされていない属性に注意してください。こちらで確認してください。caniuse.com
現在作業中のプロジェクトでは、非表示のiFrameがあり、ダウンロードダイアログボックスを表示するには、ファイルのURLをiFrameにフィードする必要がありました。ボタンをクリックすると、コントローラーが動的URLを生成し、directive私が記述したカスタムがリストされている$ scopeイベントをトリガーします。ディレクティブは、iFrameがまだ存在しない場合は本体に追加し、それにurl属性を設定します。
編集:ディレクティブを追加する
appModule.directive('fileDownload', function ($compile) {
var fd = {
restrict: 'A',
link: function (scope, iElement, iAttrs) {
scope.$on("downloadFile", function (e, url) {
var iFrame = iElement.find("iframe");
if (!(iFrame && iFrame.length > 0)) {
iFrame = $("<iframe style='position:fixed;display:none;top:-1px;left:-1px;'/>");
iElement.append(iFrame);
}
iFrame.attr("src", url);
});
}
};
return fd;
});
このディレクティブは、呼び出されたコントローラーイベントに応答します。 downloadFile
だからあなたのコントローラーでは
$scope.$broadcast("downloadFile", url);
あなたは、設定することができますlocation.hrefにデータURIは、ユーザーのダウンロードをできるようにするデータを含みます。これ以外に、JavaScriptだけでそれを行う方法はないと思います。
$location.href変更$window.location.href
unsafe:blob:nullが原因でファイルがダウンロードされない場合に備えて、それを追加したいだけです。ダウンロードボタンにカーソルを合わせると、サニタイズする必要があります。例えば、
var app = angular.module( 'app'、[]);
app.config(function($ compileProvider){
$compileProvider.aHrefSanitizationWhitelist(/^\s*(|blob|):/);
サーバー上でにアクセスできる場合は、このより一般的な質問で回答されているようにヘッダーを設定することを検討してください。
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"
その回答に関するコメントを読んで、オクテットストリームよりも具体的なコンテンツタイプを使用することをお勧めします。
同じ問題があり、別の解決策を見つけるのに何時間も費やしましたが、今はこの投稿のすべてのコメントに参加します。
HTML:
<a href="#" class="btn btn-default" file-name="'fileName.extension'" ng-click="getFile()" file-download="myBlobObject"><i class="fa fa-file-excel-o"></i></a>
指令:
directive('fileDownload',function(){
return{
restrict:'A',
scope:{
fileDownload:'=',
fileName:'=',
},
link:function(scope,elem,atrs){
scope.$watch('fileDownload',function(newValue, oldValue){
if(newValue!=undefined && newValue!=null){
console.debug('Downloading a new file');
var isFirefox = typeof InstallTrigger !== 'undefined';
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
var isIE = /*@cc_on!@*/false || !!document.documentMode;
var isEdge = !isIE && !!window.StyleMedia;
var isChrome = !!window.chrome && !!window.chrome.webstore;
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isBlink = (isChrome || isOpera) && !!window.CSS;
if(isFirefox || isIE || isChrome){
if(isChrome){
console.log('Manage Google Chrome download');
var url = window.URL || window.webkitURL;
var fileURL = url.createObjectURL(scope.fileDownload);
var downloadLink = angular.element('<a></a>');//create a new <a> tag element
downloadLink.attr('href',fileURL);
downloadLink.attr('download',scope.fileName);
downloadLink.attr('target','_self');
downloadLink[0].click();//call click function
url.revokeObjectURL(fileURL);//revoke the object from URL
}
if(isIE){
console.log('Manage IE download>10');
window.navigator.msSaveOrOpenBlob(scope.fileDownload,scope.fileName);
}
if(isFirefox){
console.log('Manage Mozilla Firefox download');
var url = window.URL || window.webkitURL;
var fileURL = url.createObjectURL(scope.fileDownload);
var a=elem[0];//recover the <a> tag from directive
a.href=fileURL;
a.download=scope.fileName;
a.target='_self';
a.click();//we call click function
}
}else{
alert('SORRY YOUR BROWSER IS NOT COMPATIBLE');
}
}
});
}
}
})
コントローラー:
$scope.myBlobObject=undefined;
$scope.getFile=function(){
console.log('download started, you can show a wating animation');
serviceAsPromise.getStream({param1:'data1',param1:'data2', ...})
.then(function(data){//is important that the data was returned as Aray Buffer
console.log('Stream download complete, stop animation!');
$scope.myBlobObject=new Blob([data],{ type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
},function(fail){
console.log('Download Error, stop animation and show error message');
$scope.myBlobObject=[];
});
};
稼働中:
function getStream(params){
console.log("RUNNING");
var deferred = $q.defer();
$http({
url:'../downloadURL/',
method:"PUT",//you can use also GET or POST
data:params,
headers:{'Content-type': 'application/json'},
responseType : 'arraybuffer',//THIS IS IMPORTANT
})
.success(function (data) {
console.debug("SUCCESS");
deferred.resolve(data);
}).error(function (data) {
console.error("ERROR");
deferred.reject(data);
});
return deferred.promise;
};
バックエンド(春):
@RequestMapping(value = "/downloadURL/", method = RequestMethod.PUT)
public void downloadExcel(HttpServletResponse response,
@RequestBody Map<String,String> spParams
) throws IOException {
OutputStream outStream=null;
outStream = response.getOutputStream();//is important manage the exceptions here
ObjectThatWritesOnOutputStream myWriter= new ObjectThatWritesOnOutputStream();// note that this object doesn exist on JAVA,
ObjectThatWritesOnOutputStream.write(outStream);//you can configure more things here
outStream.flush();
return;
}
これは角度で私のために働きました:
var a = document.createElement("a");
a.href = 'fileURL';
a.download = 'fileName';
a.click();
data:text/plain;base64,${btoa(theStringGoesHere)}