如何显示图像? [英] how to display the image?

查看:84
本文介绍了如何显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我在mvc 4中进行的项目中,用户上传图片,当用户点击创建按钮时,将显示该图片。不幸的是,它没有播放图像,只能上传图片。通过给出名字,例如,4.jpg。我该怎么做才能拍照。

//在我的控制器中

公共ActionResult创建()

I wanted in my project which I am doing in mvc 4, user upload the picture and that picture will be display when the user click on create button. unfortunetely it does not didplay the image its only upload the picture. by giving name, exmple, 4.jpg. How can I didplay the picture.
// in my controller
public ActionResult Create()

     {
            if (Session["login"] != null)
            {
                string trueLogin = Session["login"].ToString();
                if (trueLogin.Equals("true"))
                {
                    return View();
                }
            }
            return RedirectToAction("Login", "User");
        }

        //
        // POST: /Donate/Create

        [HttpPost]
        public ActionResult Create(Donate donate)
        {
            var imageFileName = "";
            var imageFilePath = "";

            if (donate.ImageFile != null && donate.ImageFile.ContentLength > 0)
            {
                imageFileName = Path.GetFileName(donate.ImageFile.FileName);
                imageFilePath = Path.Combine(Server.MapPath("~/Content/Donateimages"), imageFileName);
                donate.ImageFile.SaveAs(imageFilePath);
            }

            donate.UploadImage = imageFileName.ToString();
            if (ModelState.IsValid)
            {
                db.Donations.Add(donate);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(donate);
        }
// in my view
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ItemType)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ItemDescription)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date)
        </th>
       
        <th>
            @Html.DisplayNameFor(model => model.UploadImage)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ItemType)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ItemDescription)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <
        <td>
            @Html.DisplayFor(modelItem => item.UploadImage)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

推荐答案

查看为数据库保存的byte []和从字节数组中显示的图像。文件夹路径对每个人来说都很简单,这也是。但出于演示目的,我将通过varbinary格式。当我来显示一个人的细节时,我会卡在显示图像上。我在Controller中执行了以下方法 - >

See byte[] saved for database and image to display from a byte array. Where folder path will simple for everyone and this is also. But for demo purposes, I am going through varbinary format. When I come to display the details for a Person, I get stuck on displaying the Image. I did the following method in Controller ->
public FileContentResult getImg(int id)
{
    byte[] byteArray = db.Persons.Find(id).Photo;
    if (byteArray != null)
    {
        return new FileContentResult(byteArray, "image/jpeg");
    }
    else
    {
        return null;
    }
}



观看次数 - >设计


Views -> Design

<img src="@Url.Action(" getimg=", " controls_=", new { id = Model.ID })" alt="Person Image" />







从以下链接获取更多详情

< a href =http://www.codeproject.com/Articles/686768/Using-Controls-are-Dropdownlist>使用控件是Dropdownlist,RadioButtonList,DateTime Calender,FileUpload,Cascading Dropdown [ ^ ]


这篇关于如何显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆