Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How client download excel file implement #20

Open
nvcken opened this issue Sep 29, 2014 · 5 comments
Open

How client download excel file implement #20

nvcken opened this issue Sep 29, 2014 · 5 comments

Comments

@nvcken
Copy link

nvcken commented Sep 29, 2014

My server code
var file = fs.readFileSync('./res_report/output/out.xlsx', 'binary');
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', "attachment; filename=" + "out.xlsx")
res.end(file, 'binary');
How client javascript download excel file

@functionscope
Copy link
Owner

try

window.open(replaceWithYourUrl);

On Mon, Sep 29, 2014 at 4:49 AM, nvcken [email protected] wrote:

My server code
var file = fs.readFileSync('./res_report/output/out.xlsx', 'binary');
res.setHeader('Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', "attachment; filename=" + "out.xlsx")
res.end(file, 'binary');
How client javascript download excel file


Reply to this email directly or view it on GitHub
#20.

@nvcken
Copy link
Author

nvcken commented Sep 29, 2014

@functionscope
var file = fs.readFileSync('./res_report/output/out.xlsx', 'binary');
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', "attachment; filename=" + "out.xlsx")
res.end(file, 'binary');
client I use POST method request to server and server response by above server code
My client code angularjs
$http.post('/abc/excel', post).success(function(data){
var blob = new Blob([data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
})
.error(function( data){
})

but not work, file open corrupt. What I am wrong

@nvcken
Copy link
Author

nvcken commented Sep 29, 2014

If I do the same with csv file it work

@functionscope
Copy link
Owner

check out this link

http://stackoverflow.com/questions/22447952/angularjs-http-post-convert-binary-to-excel-file-and-download

On Sep 29, 2014 7:57 AM, "nvcken" [email protected] wrote:

If I do the same with csv file it work


Reply to this email directly or view it on GitHub.

@rajan-g
Copy link

rajan-g commented Sep 1, 2016

Hi, Me also meet this issue after spent long time I found solution, It is working for me.
setup's
1.convert your file as base64 string and send as response
EXAMPLE:
var result = nodeExcel.execute(conf);
return res.status(200).send(new Buffer(result.toString(), 'binary').toString('base64'));
2.client side decodebase64 string, make as blob and create url from blob
EXAMPLE:
var contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
var sliceSize = sliceSize || 512;

                var byteCharacters = atob(data);
                var byteArrays = [];

                for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
                  var slice = byteCharacters.slice(offset, offset + sliceSize);

                  var byteNumbers = new Array(slice.length);
                  for (var i = 0; i < slice.length; i++) {
                    byteNumbers[i] = slice.charCodeAt(i);
                  }
                  var byteArray = new Uint8Array(byteNumbers);

                  byteArrays.push(byteArray);
                }
                var blob = new Blob(byteArrays, {type: contentType});
                var blobUrl = URL.createObjectURL(blob);
                window.location = blobUrl;

Enjoy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants