0

I'm developing a report in Excel, from a WebPage. The page have some chart's and some data inside some tables. I have an excel file, that works as a model to write data inside the file. I created some chart´s inside the sheet, and write data inside some sheet, and i reference this data to use it on chart's. Everything works well, except by the fact that I cannot download the file.

I created a piece of code to do this, and I generated the file successfully. What I'm trying to do now, is download this file.

So i tried something:

So, I generate the file, and the file as been downloaded, but comes corrupted.

API

var mimeType = "application/vnd.ms-excel";

string file = Path.Combine(path, fileName);

var memory = new MemoryStream();

using (var stream = new FileStream(file, FileMode.Open))
{
    stream.CopyTo(memory);
}

memory.Position = 0;

return File(memory, mimeType, "filename.xlsx");

Angular

return $http.post(url, data, config)
    .then(function (response) {

    //TODO: Implementar o download do arquivo
    var file = new Blob([data], { type: 'application/vnd.ms-excel' });
    saveAs(file, 'filename.xlsx');

    }).catch(function (error) {
        console.log("error");
    });
7
  • I dont think so thatwe are speaing about the same thing. I'm using Angular1, and the solution that you are trying to give me is talking about a solution writed in Angular2. Did you read my text? Commented Jan 21, 2019 at 14:30
  • What do you mean "the file is corrupted"? And what is the code of saveAs(file, 'filename.xlsx') function? Commented Jan 21, 2019 at 14:32
  • @Bisneto There is nothing in your post that says anything about Angular1. The tag you chose was for Angular 2 and above. I have fixed that. Thanks for the clarification. I don't know that it changes anything though. I don't see anything in your question that appears to be Angular1 specific. Commented Jan 21, 2019 at 14:35
  • Hi Alexander. I created the file, and tryied to open by Excel. It´s ok, he opened. When i send this by memory stream to Angular, as you can read on the code that i attached, then i receive the message : "Excel cannot open the file "filename". Verify if the file is corrupted or if the extension of file is invalid" Commented Jan 21, 2019 at 14:35
  • Ah, ok, sorry, i thought that ommiting number beside of Angular you would understand. Just to let you know, I'm talking about a solution using Angular1. Thanks ! Commented Jan 21, 2019 at 14:38

1 Answer 1

0

It looks like you have an error in this line

var file = new Blob([data], { type: 'application/vnd.ms-excel' });

Try to change it to

var file = new Blob([response.data], { type: 'application/vnd.ms-excel' });

Also you can try the following code. It works just fine

var linkElement = document.createElement('a');
try {
    var blob = new Blob([response.data], { type: 'application/vnd.ms-excel'});
    var url = window.URL.createObjectURL(blob);

    linkElement.setAttribute('href', url);
    linkElement.setAttribute("download", filename);

    var clickEvent = new MouseEvent("click", {
        "view": window,
        "bubbles": true,
        "cancelable": false
    });
    linkElement.dispatchEvent(clickEvent);

    } 
    catch (ex) {
        console.log(ex);
    }

Source: http://jaliyaudagedara.blogspot.com/2016/05/angularjs-download-files-by-sending.html

Sign up to request clarification or add additional context in comments.

19 Comments

I have try this change: return $http.post(url, data, config) .then(function (response) { //TODO: Implementar o download do arquivo var file = new Blob([response.data], { type: 'application/vnd.ms-excel' }); saveAs(file, 'filename.xlsx'); }).catch(function (error) { console.log("error"); }); But the error is the same.
@Bisneto Hmm, so it should be something wrong inside saveAs function. I will be able to say more if I see the code.
It doesn't ocurr any error during execution of script. How i could send you the code to you take a look ?
@Bisneto I've updated my answer. Try this solution. If it doesn't work please edit you question by adding the code of the saveAs function or maybe whole javascript file code.
How i can send you a print screen ? It´s coming data from data property of response. I think is sometinng really software
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.