Jump to content

Calling Dev Gurus - JS Save File Help required


Recommended Posts

mickle026

I have built a movie report as a list in my c# routine and passing it back to JS as an object

 

Like this, so that the download link appears in my html placeholder <p id="downloadMovieList"></p>

        ApiClient.MovieReport = function () {

            ApiClient.getJSON(ApiClient.getUrl('CustomImageServer/MovieReport')).then(response => {
                console.log(response);
                var mov = response;
                var htmlstring = "<a download=\"MovieReport.csv\" href=\"data:application/octet-stream,\"" + encodeURIComponent(mov.MovieText) + "\">Click to Download</a>";
                document.getElementById("downloadMovieReport").innerHTML = htmlstring;
            }).catch(e => {
                console.log(e);
            });
        }

The function works fine except that my data file is 0 bytes

 

Doing it this way:

        ApiClient.MovieReport = function () {

            ApiClient.getJSON(ApiClient.getUrl('CustomImageServer/MovieReport')).then(response => {
                console.log(response);
                var mov = response;
                uriContent = "data:application/octet-stream," + encodeURIComponent(mov.MoviesText);
                newWindow = window.open(uriContent, "MovieReport.txt");

            }).catch(e => {
                console.log(e);
            });
        }

Results in all the data, but the window.open doesn't show the filename and saves as a raw octet-stream with a temporary filename and no extension.

 

I much prefer the top method but why is there no data when the response is a full list object.

 

Its got to be a quote or something thats in the wrong place, but its one of them I've been trying for ages and still cannot see it.

Pretty confident that it is somewhere here:

 href=\"data:application/octet-stream,\"" + encodeURIComponent(mov.MovieText) + "\">Click to Download</a>";

even changing it to   href=\"data:text/html,\"" + encodeURIComponent(mov.Movies) + "\">Click to Download</a>";

 

Has no effect on the 0 data length, only the file type it tries to save as.

 

@@chef

Link to post
Share on other sites
mickle026

Sorted it :), it was the \" after the octet-stream, making the encoded text become after a quote.

 

changed from

href=\"data:application/octet-stream,\"" + encodeURIComponent(mov.MovieText) + "\">Click to Download</a>";

to
 

 href=\"data:application/octet-stream," + encodeURIComponent(mov.MovieText) + "\">Click to Download</a>";

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...