Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send image created by image2canvas to controller using ajax and then download it using controller.

Here is my code:

JavaScript
$("#btnExportAsImageByPost").click(function () {
            var base64;
            html2canvas($("#Table")[0]).then(function (canvas) {
                base64 = canvas.toDataURL();
                document.body.appendChild(canvas);
                alert(base64);
                $("[id*=hfImageData]").val(base64);
            });
            alert(base64);
            var url = "/HtmlToImage/Index/";
            $.ajax({
                type: 'POST',
                url: url,
                data: base64,
                dataType: "string",
                success: function (evt) {
                    $("#Table").hide('slow');
                },
            });
        });



HTML
<div id="Table" style="width: 340px; background-color: White; padding: 5px">
    <table cellspacing="0" border="1">
        <tbody><tr>
            <th style="width: 90px">Customer id</th>
            <th style="width: 120px">Name</th>
            <th style="width: 120px">Country</th>
        </tr>
        <tr>
            <td>1</td>
            <td>John Hammond</td>
            <td>United States</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Mudassar Khan</td>
            <td>India</td>
        </tr>
    </tbody></table>
</div>
<div>
    <input type="button" name="btnExportAsImageByPost" id="btnExportAsImageByPost" value="Export as image(Post)" />
</div>


Here is my controller action:


C#
[HttpPost]
[ActionName("Index")]
public ActionResult Index_Post(string base64)
{
   byte[] bytes = Convert.FromBase64String(base64);
    return File(bytes, "image/png", "HtmlToImage.png");
}


But problem is nothing happens in browser when the controller returns file. Instead of prompting to save file.

What I have tried:

The above code is what I tried till now
Posted
Updated 1-Nov-16 8:52am
v2

try this:

$.ajax({
                type: 'POST',
                url: url,
                data: { base64: base64 },
                dataType: "string",
                success: function (evt) {
                    $("#Table").hide('slow');
                },
            });


Also your URL does not seem to match the controller name
 
Share this answer
 
v2
First problem:
Looking at your code, the html2canvas function returns a Promise[^].

The callback function passed to then won't be invoked immediately; it will be invoked when the function has completed.

Therefore, when you go to post the data, the base64 variable will be blank, and you will be sending an empty string to your controller.

To solve that, you need to move the remainder of your function to the callback:
JavaScript
$("#btnExportAsImageByPost").click(function () {
    html2canvas($("#Table")[0]).then(function (canvas) {
        var base64 = canvas.toDataURL();
        document.body.appendChild(canvas);
        alert(base64);
        $("[id*=hfImageData]").val(base64);
        
        var url = "/HtmlToImage/Index/";
        $.ajax({
            ...
        });
    });
});


Second problem:
As pointed out in Solution 1, you need to post an object with the correct parameter name:
JavaScript
$.ajax({
    type: 'POST',
    url: url,
    data: { base64: base64 },
    dataType: "string",
    success: function (evt) {
        $("#Table").hide('slow');
    }
});


Third problem:
The string returned from the canvas.toDataURL() method[^] will be a data URI[^] - something that looks like:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAADElEQVQImWNgoBMAAABpAAFEI8ARAAAAAElFTkSuQmCC

If you try to pass that string to Convert.FromBase64String, you will get an exception, because it is not a valid Base64-encoded string.

You need to remove the data:image/png;base64, prefix from the start of the string first.
C#
[HttpPost]
[ActionName("Index")]
public ActionResult Index_Post(string base64)
{
    int index = base64.IndexOf(',');
    if (index != -1) base64 = base64.Substring(index + 1);
    
    byte[] bytes = Convert.FromBase64String(base64);
    return File(bytes, "image/png", "HtmlToImage.png");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900