asp.net mvc - Excel file not saved in user's PC -


i have tried download excel file server user's pc. have following codes internet. have modified codes bit....

but when run code, nothing happens. save dialog box not popping up. want happen excel file server saved in user's pc popping save dialog box.

please help!!!

public void downloadfile(string url) {     string path = server.mappath("~/images/0ca66926-6977-43d3-9c97-f84a43f6ce5d.xls");     var file = new fileinfo(path);     string outgoingfile = "filename.xlsx";     if (file.exists)     {         response.clear();         response.clearcontent();         response.clearheaders();         response.addheader("content-disposition", "attachment; filename=" + outgoingfile);         response.addheader("content-length", file.length.tostring());         response.contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";         response.writefile(file.fullname);         response.flush();         response.close();      }     else     {         response.write("this file not exist.");     } }  

$.ajax({     url: '@url.action("downloadfile", "home")',     type: 'post',     contenttype: "application/json",     data: json.stringify({ url: urllink}),     success: function(res) {      },     error: (function() {         alert("error retrieve selected report");     }) }); 

since looking working download file code, sharing use.

we have 2 call process download code. first call file prepared , saved on server. second call shown below read file content , return file object.

controller.cs:

    [httpget]     public actionresult downloadstream(string filename, int searchid)     {         var tempstore = _searchservice.gettempstore(searchid);         var bytes = tempstore.dataasblob;         if (bytes != null)         {             var stream = new memorystream(bytes);             _searchservice.deletetempstore(searchid);             return file(stream, "application/vnd.ms-excel", filename);         }          _logger.log("export/downloadstream request failed", logcategory.error);         return json("failed");     } 

view.cshtml

following line calls downloadstream method on server, server send file object gets save on user machine.

window.location = '/export/downloadstream?filename=' + result+"&searchid="+searchid;

    function exportdata(exporttype, exportformat) {          var searchid = document.getelementbyid('hfselecteddriverid').value;         var model = { searchid: searchid, exporttype: exporttype, exportformat: exportformat };          $.ajax({             url: '@url.action("exportdriverfile", "export")',             contenttype: 'application/json; charset=utf-8',             type: 'post',             datatype: 'html',             data: json.stringify(model)         })             .success(function (result) {                 result = result.tostring().replace(/"/gi, "");                 if (result == "" || result == "failed")                 {                     alert("file download failed. please try again!");                 }                 else                 {                     window.location = '/export/downloadstream?filename=' + result+"&searchid="+searchid;                 }             })         .error(function (xhr, status) {             //alert(status);         });     } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -