मैं एक छिपे हुए textarea में कुछ पाठ है। जब एक बटन पर क्लिक किया जाता है तो मैं एक .txtफ़ाइल के रूप में डाउनलोड के लिए दिए गए पाठ की पेशकश करना चाहूंगा । क्या यह एंगुलरजेएस या जावास्क्रिप्ट का उपयोग करना संभव है?
मैं एक छिपे हुए textarea में कुछ पाठ है। जब एक बटन पर क्लिक किया जाता है तो मैं एक .txtफ़ाइल के रूप में डाउनलोड के लिए दिए गए पाठ की पेशकश करना चाहूंगा । क्या यह एंगुलरजेएस या जावास्क्रिप्ट का उपयोग करना संभव है?
जवाबों:
आप इस तरह से कुछ का उपयोग कर सकते हैं 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.revokeObjectURL () पर कॉल करके जारी किया जाना चाहिए, जब आपको उनकी आवश्यकता नहीं होगी। दस्तावेज़ अनलोड होने पर ब्राउज़र स्वचालित रूप से इन्हें जारी करेगा; हालाँकि, इष्टतम प्रदर्शन और स्मृति उपयोग के लिए, यदि कोई सुरक्षित समय है जब आप उन्हें स्पष्ट रूप से अनलोड कर सकते हैं, तो आपको ऐसा करना चाहिए।
स्रोत: एमडीएन
$scope.urlमेरे लिए काम नहीं किया। मुझे window.locationइसके बजाय उपयोग करना था।
downloadविशेषता किसी भी IE या सफारी संस्करणों में समर्थित नहीं है, हालांकि 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">
और इस साइट पर जाएँ यह आपके लिए उपयोगी हो सकता है :)
downloadविशेषता से सावधान रहें जो अभी भी किसी भी IE या सफारी संस्करण द्वारा समर्थित नहीं है। इसे यहाँ देखें: caniuse.com/#feat=download
यह दूसरी ब्राउज़र विंडो खोलने की आवश्यकता के बिना जावास्क्रिप्ट में किया जा सकता है।
window.location.assign('url');
अपनी फ़ाइल के लिंक के साथ 'url' बदलें। आप इसे एक फ़ंक्शन में रख सकते हैं और इसे कॉल कर सकते हैं ng-clickयदि आपको एक बटन से डाउनलोड को ट्रिगर करने की आवश्यकता है।
काम पर हमारी वर्तमान परियोजना में हमारे पास एक अदृश्य iFrame था और मुझे एक डायलॉग बॉक्स डाउनलोड करने के लिए iFrame में फ़ाइल के लिए url फीड करना था। बटन पर क्लिक करने पर, नियंत्रक गतिशील यूआरएल उत्पन्न करता है और एक $ गुंजाइश घटना को ट्रिगर करता है जहां एक कस्टम directiveमैंने लिखा है, सूचीबद्ध है। यदि यह पहले से ही मौजूद नहीं है और इस पर url विशेषता सेट करता है, तो निर्देश शरीर को एक iFrame संलग्न करेगा।
संपादित करें: एक निर्देश जोड़ना
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एक डेटा यूआरआई सेट कर सकते हैं जिसमें वह डेटा है जिसे आप उपयोगकर्ता को डाउनलोड करने देना चाहते हैं। इसके अलावा, मुझे नहीं लगता कि जावास्क्रिप्ट के साथ ऐसा करने का कोई तरीका है।
$location.hrefबदलकर$window.location.href
केवल यह जोड़ना चाहते हैं कि यदि असुरक्षित के कारण यह फ़ाइल डाउनलोड नहीं करता है: blob: null ... जब आप डाउनलोड बटन पर होवर करते हैं, तो आपको इसे sanitize करना होगा। उदाहरण के लिए,
var app = angular.module ('ऐप', []);
app.config (समारोह ($ compileProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(|blob|):/);
यदि आपके पास सर्वर पर पहुंच है, तो हेडर सेट करने पर विचार करें जैसा कि इस अधिक सामान्य प्रश्न में उत्तर दिया गया है ।
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"
उस उत्तर पर टिप्पणियों को पढ़ना, ओकटेट-स्ट्रीम की तुलना में अधिक विशिष्ट सामग्री-प्रकार का उपयोग करना उचित है।
मेरे पास एक ही समस्या थी और कई घंटे अलग-अलग समाधान खोजने में खर्च करते थे, और अब मैं इस पोस्ट में सभी टिप्पणियों को शामिल करता हूं। मुझे आशा है कि यह उपयोगी होगा, मेरा जवाब सही ढंग से इंटरनेट एक्सप्लोरर 11, क्रोम और फायरफॉक्स पर परीक्षण किया गया था।
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;
}
मैं स्टैटिक उरल नहीं चाहता था। मेरे पास सभी अजाक्स संचालन करने के लिए AjaxFactory है। मैं कारखाने से url प्राप्त कर रहा हूं और इसे निम्नानुसार बांध रहा हूं।
<a target="_self" href="{{ file.downloadUrl + '/' + order.OrderId + '/' + fileName }}" download="{{fileName}}">{{fileName}}</a>
साभार @AhlemMustapha