从get请求ajax调用中获取图像时出现服务器错误

时间:2018-05-21 09:30:57

标签: jquery html asp.net ajax web

执行Ajax获取从mvc asp.net的另一个视图获取图像的请求时出现以下控制台错误,函数未达到成功方法:

"GET http://localhost:1879/cardimages/getimage?id=12 500 (Internal Server Error)"

下面我要添加控制器的所有三个代码片段,ajax调用和下拉列表的查看代码,我试图从db中获取图像

我正在尝试从asp.net mvc中的下拉列表选择中获取图像:

以下是AJAX代码:

$(function () {
    $("#ImageId").change(function (evt) {
        var DropDownSelectedVal = $("#ImageId :selected").val();
        alert(DropDownSelectedVal);
        if (DropDownSelectedVal != null) {
            alert("Success");
            $.ajax({
                url: "@Url.Action("GetImage", "CardImages")",
                type: "Get",
                dataType: 'json',
                contentType: "Image",
                data: {id: DropDownSelectedVal},
                success: function (data) {
                    alert("Success");
                    $("img-upload2").attr('src', data);
                },
                error: function (xhr) {
                    alert("Something went wrong, please try again");
                }
            });
        }
    });
});

以下是控制器中的功能:

public ActionResult GetImage(int id /* drop down value */)
{
    var cardImage = db.CardImages.Where(x=>x.CityId == id ).FirstOrDefault();
    string dir = ConfigurationManager.AppSettings["UploadFilesPath1"].ToString() + "/";
    string logoPath = GetFileFromFolder(dir, cardImage.CardFileName + cardImage.OriginalFileName);
    if (logoPath != string.Empty)
    {
        Byte[] bytes = System.IO.File.ReadAllBytes(logoPath);
        cardImage.LogoImage = "data:image/jpeg;charset=utf-8;base64," + Convert.ToBase64String(bytes);
    }
    //var model = db.CardImages.Find(id); // This is for example put your code to fetch record.
    //ViewBag.img = (from s in db.CardImages where s.CityId == id select s.CardFileName + s.OriginalFileName);
    return Json(cardImage.LogoImage);
}

鉴于此下拉列表我正在使用

@Html.DropDownList("CityId", null,  "Select City", htmlAttributes: new { @class = "form-control", @Id = "ImageId" })

我发现浏览器出现警告错误,因为出现问题和控制台错误,如上所述。

1 个答案:

答案 0 :(得分:0)

UIView