1

我的项目是 asp.net MVC 3,我使用以下脚本导出画布数据:

 $("#savePNG").click(function () {
                stage.toDataURL({
                    callback: function (dataUrl) {
                        dataUrl = dataUrl.replace('data:image/png;base64,', '');
                        $.ajax({
                        type: 'POST',
                        url: "../../Home/UploadImage",
                        data: '{ "imageData" : "' + dataUrl + '" }',
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function(msg) {
                            alert('Image saved successfully !');
                        }
                    });
                }
                });
            });

控制器脚本:

           public FileContentResult UploadImage(string imageData){
            byte[] data = Convert.FromBase64String(imageData);
                   return File(data, "image/png", "test.png");
        }

它不起作用,任何建议,提前谢谢。

4

1 回答 1

1

这是我的解决方案,以防有人试图做同样的事情

在控制器中:

   public JsonResult UploadImage(string imageData)
        {
            byte[] data = Convert.FromBase64String(imageData);
            MySession.Current._pngtoPrint = data;

            return Json(new { success = true }, JsonRequestBehavior.AllowGet);
        }

        public FileContentResult ExportPng()
        {
            byte[] contents =  MySession.Current._pngtoPrint;
            return File(contents, "image/png", "test" + ".png");
        }

在视图中:

  stage1.toDataURL({
                    callback: function(dataUrl) {
                        dataUrl = dataUrl.replace('data:image/png;base64,', '');
                        $.ajax({
                            type: 'POST',
                            url: "../../Home/UploadImage",
                            data: '{ "imageData" : "' + dataUrl + '" }',
                            contentType: 'application/json; charset=utf-8',
                            dataType: 'json',
                            success: function(data) {
                               // alert('Image saved successfully !');
                                window.location("Home/ExportPng");
                            }
                        });
                    }
                });
于 2013-05-18T05:08:49.103 回答