【问题标题】:how to get the image name when click on edit button?单击编辑按钮时如何获取图像名称?
【发布时间】:2020-07-01 14:19:30
【问题描述】:

当我获取上传的图片名称时出现错误,请参阅控制台

我在数据库中成功上传了图片,但现在我想在点击编辑按钮时获取上传的图片名称

student.cs

namespace AxInMvc.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class student
    {

       public int studentid { get; set; }
       public string studentname { get; set; }
       public string studentimage { get; set; }

HomeController.cs

        public JsonResult GetbyID(int studentid)
        {
            return Json(InsAjaxEntities.students.FirstOrDefault(x => x.studentid == studentid), JsonRequestBehavior.AllowGet);
        }

索引.cshtml

<div class="form-group">
    <label for="studentname"><strong>StudentName:</strong></label>
    <input type="text" id="studentname" class="form-control" />
</div>


<div class="form-group">
  <label for="studentimage"><strong>StudentImage:</strong></label>
  <input id="studentimage" type="file" name="file"/>
</div>


@section scripts {
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    <script type="text/javascript">

    function getbyID(studentid) {

            debugger

            $.ajax({

                type: "GET",
                url: "/Home/GetbyID" + "?" + "studentid=" + studentid,
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                success: function (result) {

                    debugger

                    $('#studentid').val(result.studentid);
                    $('#studentname').val(result.studentname);

                    $('#studentimage').val(result.studentimage); //I think here I am getting error here
                    
                    $('#myModal').modal('show'); //here I am get the data and then updating the data
                    $('#btnUpdate').show();
                    $('#btnAdd').hide();

                },
                error: function (errormessage) {
                    debugger
                    console.log(JSON.stringify(errormessage));

                }
            });
        }

当我得到上传的图片名称时会报错:

https://i.stack.imgur.com/ssUug.png

请帮忙?

【问题讨论】:

    标签: javascript jquery ajax asp.net-mvc


    【解决方案1】:

    您的&lt;input&gt; 属于type="file"

    <input id="studentimage" type="file" name="file"/>
    

    由于错误状态,浏览器不允许您在此处设置任何值,而不是空字符串。所以这个操作失败了:

    $('#studentimage').val(result.studentimage);
    

    当您退后一步思考时,这是有道理的。您的数据中有一个服务器文件,但&lt;input type="file"&gt; 不读取服务器的文件系统,它读取客户端的文件系统。这是您无法从 Web 应用程序中访问的内容。

    (另外,想象一个互联网,您可以将其设置为可能存在于用户计算机上的文件,例如常见的系统文件。您访问的任何网站都可以自动抓取在您不知情的情况下从您的计算机中获取文件。这将是一件非常糟糕的事情。)

    您可以向用户显示您拥有的文件,例如图像缩略图或类似性质的东西。您可以向用户显示 &lt;input type="file"&gt; 以提交新文件,但您不能使用已有文件填充该输入。

    基本上只需删除该行 JavaScript,这样您就不会尝试填充该输入。

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 2018-06-22
      • 1970-01-01
      • 2021-03-27
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多