MVC 保存二进制到数据库,并显示文件的方法(图片显示或下载文件)

1.之前我是直接上传在服务器硬盘,保存路径到数据库,但是客户有个需求就是:他们会有多个服务器(比如缓存服务器等)这样子,客户上传了一个到某一个服务器,那么如果下次跳转到另外一个服务器,那么就会找不到!

//忽略

Template upload – in DB server too. The problem is that in our production there could be multiple app server and user may not get the template image if he is redirected to a new server. Not all the time it can happen but some scenario this could be a potential risk. So please change template save in DB and not in app server and if possible please incorporate in Monday’s version.

//忽略


a.保存文件的类型呢, 在sql是Varbinary(max),而在c#是 byte[]

1)控制器

   [HttpPost]
        public JsonResult UploadAttachments()
        {
        
                   HttpPostedFileBase hpf = Request.Files["HelpSectionFiles"];
                    //fileContent 这个byte[]就是保存的值了, 可以直接保存到数据库        


                        Stream fileInStream = hpf.InputStream;
                        var fileContent = new byte[hpf.ContentLength];
                        int iStatus = fileInStream.Read(fileContent, 0, hpf.ContentLength);
                        fileInStream.Flush();
                        fileInStream.Close();
                        string fileName = hpf.FileName;
                        if (fileName.LastIndexOf("\\") != -1)
                        {
                            fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                        }
                     

                        return actionResult;
        }



b.读取

1).前台shtml:   通过img 的 src调用

 <img width="172" height="275" src="/CreateEmail/GetTemplateThumb?TemplateId=@TemplateModel.TemplateId" alt="@TemplateModel.TemplateName" class="template_img" />

2)后台控制器:   直接使用  Response.BinaryWrite();

     public void GetTemplateThumb(int TemplateId)
        {
            var entity = apService.GetTemplateByIdService(TemplateId);
            if (entity != null && entity.TemplateThumb != null)
            {
                byte[] TemplateThumb = entity.TemplateThumb;
                Response.BinaryWrite(TemplateThumb);
            }

        }



reference:

http://blog.csdn.net/zhang_yang_43/article/details/51635359

非常感谢他.


这是他的


之前在http://www.cnblogs.com/JsonZhangAA/p/5568575.html博文中是利用的image控件来显示的二进制流图片,我现在想的是能

通过普通的<img id="xx" src="xx"/>这种形式来显示我的二进制流图片吗?必须可以(◑▽◐),就是写法稍微麻烦了一点,img要写成这个样子:

,对你看的没错,它的地址指向了一个aspx页面,这个页面有个奇特之处,

就是我们新建后,不用写任何前台代码,WebForm1前后台代码如下:

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="显示爬虫所爬的数据库中的图片.WebForm1" %>

后台代码:

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 显示爬虫所爬的数据库中的图片.Models;

namespace 显示爬虫所爬的数据库中的图片
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = int.Parse(Request["id"].ToString());
            DataClasses1DataContext db = new DataClasses1DataContext();
            Response.ContentType = "application/binary;";
            //这个地方图片可以从数据库中读取二进制图片  
            //byte[] img = DBHelper.ReadImg();  
            byte[] img = db.pictureUrl.Where(p=>p.Id==id).First().pictureUrl1.ToArray();
            Response.BinaryWrite(img);

            Response.Flush();
            Response.End();  
        }
    }
}
复制代码

我们主页面的前后台代码如下:

前台:

复制代码
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <%foreach(var item in ViewBag.Pictures) %>
        <%{ %>
             <img src="WebForm1.aspx?id=<%:item.Id %>" />
        <%} %>
    </div>

</body>
</html>
复制代码

后台代码:

复制代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using 显示爬虫所爬的数据库中的图片.Models;

namespace 显示爬虫所爬的数据库中的图片.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        DataClasses1DataContext db = new DataClasses1DataContext();
        public ActionResult Index()
        {
            ViewBag.Pictures = db.pictureUrl;
            return View();
        }
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值