Ho del testo in un'area di testo nascosta. Quando si fa clic su un pulsante, desidero che il testo venga offerto per il download come .txtfile. È possibile utilizzando AngularJS o Javascript?
Ho del testo in un'area di testo nascosta. Quando si fa clic su un pulsante, desidero che il testo venga offerto per il download come .txtfile. È possibile utilizzando AngularJS o Javascript?
Risposte:
Puoi fare qualcosa di simile usando Blob.
<a download="content.txt" ng-href="{{ url }}">download</a>
nel tuo controller:
var content = 'file content for example';
var blob = new Blob([ content ], { type : 'text/plain' });
$scope.url = (window.URL || window.webkitURL).createObjectURL( blob );
per abilitare l'URL:
app = angular.module(...);
app.config(['$compileProvider',
function ($compileProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|blob):/);
}]);
Si prega di notare che
Ogni volta che chiamate createObjectURL (), viene creato un nuovo URL oggetto, anche se ne avete già creato uno per lo stesso oggetto. Ognuno di questi deve essere rilasciato chiamando URL.revokeObjectURL () quando non ne hai più bisogno. I browser li rilasceranno automaticamente quando il documento viene scaricato; tuttavia, per prestazioni e utilizzo della memoria ottimali, se ci sono momenti sicuri in cui è possibile scaricarli esplicitamente, è necessario farlo.
Fonte: MDN
$scope.urlnon ha funzionato per me. Ho dovuto usare window.locationinvece.
downloadattributo non è supportato in nessuna versione di IE o Safari sebbene caniuse.com/#feat=download
Basta fare clic sul pulsante per scaricare utilizzando il seguente codice.
in html
<a class="btn" ng-click="saveJSON()" ng-href="{{ url }}">Export to JSON</a>
Nel controller
$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(...)assicurarsi di impostare responseType:'arraybuffer'come spiegato qui: stackoverflow.com/questions/21628378/...
Prova questo
<a target="_self" href="mysite.com/uploads/ahlem.pdf" download="foo.pdf">
e visita questo sito potrebbe esserti utile :)
downloadall'attributo che non è ancora supportato da nessuna versione di IE o Safari. Dai
Questo può essere fatto in javascript senza la necessità di aprire un'altra finestra del browser.
window.location.assign('url');
Sostituisci "url" con il link al tuo file. Puoi metterlo in una funzione e chiamarlo con ng-clickse devi attivare il download da un pulsante.
Nel nostro progetto attuale al lavoro avevamo un iFrame invisibile e ho dovuto fornire l'URL del file all'iFrame per ottenere una finestra di dialogo per il download. Al clic del pulsante, il controller genera l'URL dinamico e attiva un evento $ scope in cui directiveviene elencato un custom che ho scritto. La direttiva aggiungerà un iFrame al corpo se non esiste già e imposta l'attributo url su di esso.
EDIT: aggiunta di una direttiva
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;
});
Questa direttiva risponde a un evento del controller chiamato downloadFile
quindi nel tuo controller lo fai
$scope.$broadcast("downloadFile", url);
È possibile impostare location.hrefun URI di dati contenente i dati che si desidera consentire all'utente di scaricare. Oltre a questo, non credo che ci sia alcun modo per farlo solo con JavaScript.
$location.hrefmodificato in$window.location.href
Vorrei solo aggiungerlo nel caso in cui non scarica il file a causa di unsafe: blob: null ... quando passi il mouse sul pulsante di download, devi disinfettarlo. Per esempio,
var app = angular.module ('app', []);
app.config (function ($ compileProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(|blob|):/);
Se hai accesso a sul server, considera l'impostazione delle intestazioni come risposta a questa domanda più generale .
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"
Leggendo i commenti su quella risposta, è consigliabile utilizzare un Content-Type più specifico rispetto a octet-stream.
Ho avuto lo stesso problema e ho passato molte ore a trovare soluzioni diverse, e ora mi unisco a tutti i commenti in questo post. Spero possa esserti utile, la mia risposta è stata testata correttamente su Internet Explorer 11, Chrome e FireFox.
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>
DIRETTIVA:
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');
}
}
});
}
}
})
NEL CONTROLLORE:
$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=[];
});
};
IN SERVIZIO:
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;
};
BACKEND (su SPRING):
@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;
}
Questo ha funzionato per me in angolare:
var a = document.createElement("a");
a.href = 'fileURL';
a.download = 'fileName';
a.click();
data:text/plain;base64,${btoa(theStringGoesHere)}