Asp.net - 为什么Page在Ajax调用后重新加载?

时间:2018-02-10 20:24:47

标签: jquery html ajax asp.net-mvc

我正在从Form中选择画布。但是Button不在形式之内。我的图像完全按预期保存,但剩下的问题是PAGE RELOAD。

单击BUTTON以从画布保存图像时调用Ajax:

$("#upload").click(function () {
    var image = document.getElementById("ModelStudedntImageDP").toDataURL("image/png");
    image = image.replace('data:image/png;base64,', '');
    $.ajax({
        type: 'POST',
        url: "../../Admin/UploadImage",
        data: '{ "imageData" : "' + image + '" }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            alert(msg);
        }
    });
});           

按钮调用:

<button type="button" class="btn btn-primary" id="upload">Test</button>

保存图像的控制器方法:

public void UploadImage(string imageData)
{
    string filePath = Server.MapPath(Url.Content("~/Content/Images/Teacher/"));

    string fileNameWitPath = filePath + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
    using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
    {
        using (BinaryWriter bw = new BinaryWriter(fs))
        {
            byte[] data = Convert.FromBase64String(imageData);
            bw.Write(data);
            bw.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

public JsonResult UploadImage(string imageData)
{
    string filePath = Server.MapPath(Url.Content("~/Content/Images/Teacher/"));

    string fileNameWitPath = filePath + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
    using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
    {
        using (BinaryWriter bw = new BinaryWriter(fs))
        {
            byte[] data = Convert.FromBase64String(imageData);
            bw.Write(data);
            bw.Close();
        }
    }
    return Json(new { success = true, message = "Success" });
}