繁体   English   中英

如何在asp.net core v 2.0中从javascript调用PageModel方法

[英]How to call PageModel Method from javascript in asp.net core v 2.0

我正在编写一个 asp.net 核心网站,它显示了在其他地方生成的图片。 我正在其他地方轮询 webapi,我已经编写并命名为 elseware ;-)

基本上,当图像可用并上传到我的网站服务器时,我需要更新显示图像的路径。 我也在从 javascript 轮询,看看图像是否可用,当它可用时,我想转到 RazorPage 的 pageModel,并调用一个函数,最好传递图像的名称。 然后该函数应该为图像调用 elseware,并将其存储在服务器上,并返回它的本地路径。

获取以图片名称为参数的C#函数有点麻烦。

 .cs file:
  public class xxModel:PageModel {
  ...
  public string UploadImage(string imageName) {
       var image = elseware.GetImage(imageName);
       string newPath = saveImage(image);
       return newPath;
       }
  }

.cshtml file:

var pollForImage = function (imageName) {
  var imagePath = @Model.UploadImage(imageName); //<== this is where it 
                                                 // fails.. I cannot get 
                                                 // the call to work with 
                                                 // a parameter..
  $("#image").attr("src", imagePath);
} 

我不知道这是否是解决它的聪明方法,但我认为它应该是可行的。 如果我将 ImagePath 设为类上的公共属性,则可以覆盖该属性的 get 和 set 操作。 但这并不清楚。

我不想在页面上使用 OnPost/OnGet。 请注意,这是 asp.net core mvc/web api v. 2.0 -(目前很新)

我不认为这会奏效,试试这个我没有测试过,但它可能会给你一个更好的画面

步骤

  1. 保存图片

  2. 在 json 中设置路径名称大小或您想要的任何信息

    公共类 UploadFilesResult { 公共字符串名称 { 获取; 设置; } 公共整数长度{ 得到; 设置; }公共字符串类型{获取; 设置; } }

    [HttpPost] public ContentResult UploadFiles() { var r = new List();

     foreach (string file in Request.Files) { HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase; if (hpf.ContentLength == 0) continue; string savedFileName = Path.Combine(Server.MapPath("~/App_Data"), Path.GetFileName(hpf.FileName)); hpf.SaveAs(savedFileName); // Save the file r.Add(new ViewDataUploadFilesResult() { Name = hpf.FileName, Length = hpf.ContentLength, Type = hpf.ContentType }); } // Returns json return Content("{\\"name\\":\\"" + r[0].Name + "\\",\\"type\\":\\"" + r[0].Type + "\\",\\"size\\":\\"" + string.Format("{0} bytes", r[0].Length) + "\\"}", "application/json");

    }

要解析图像并显示有关 iamge 的信息,请解析 json

<script type="text/javascript">
    $(document).ready(function () {
        $('#fileupload').fileupload({
            dataType: 'json',
            url: '/Home/UploadFiles',
            autoUpload: true,
            done: function (e, data) {
                $('.file_name').html(data.result.name);
                $('.file_type').html(data.result.type);
                $('.file_size').html(data.result.size);
            }
        }).on('fileuploadprogressall', function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('.progress .progress-bar').css('width', progress + '%');
        });
    });
</script>

HTML 或简单的 div

<div class="file_name">div>
<br />
<div class="file_type">div>
<br />
<div class="file_size">div>

正如 Brad Patton 所建议的,我将使用 ajax 调用与我的控制器取得联系。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM