簡體   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