Tuesday, May 8, 2012

Download selected file using ECMA

function downloadFile() {

var clientContext= SP.ClientContext.get_current();

var currentlibid = SP.ListOperation.Selection.getSelectedList();


if(currentlibid==null){
alert('Please select at least one item and try again.');
window.location.href = window.location.href;
}
else
{
var oList = clientContext.get_web().get_lists().getById(currentlibid);
var selectedItems = SP.ListOperation.Selection.getSelectedItems(clientContext);

if(selectedItems==''){
alert('Please select at least one item and try again.');
window.location.href = window.location.href;
}
else if(selectedItems.length>1){
alert('Please select only one item and try again.');
}
else{
  

    for(var i in selectedItems){

    this.oListItem = oList.getItemById(selectedItems[i].id);
    clientContext.load(this.oListItem);

    var File = this.oListItem.get_file();

clientContext.load(File);

 
clientContext.executeQueryAsync
(
function (sender, args) {
if(File != null) {

var urlValue=File.get_serverRelativeUrl();

STSNavigate('/_layouts/download.aspx?SourceUrl=' + urlValue);

clientContext.executeQueryAsync(
function (sender, args) {
 
 
},
function (sender, args) {
 
});
}
},
function (sender, args) {
  alert('You cannot download a folder. Please select a file and try again.');
 
}
);
 
    
}
}
}
}

No comments:

Post a Comment