MVC 保存二进制到数据库,并显示文件的方法(图片显示或下载文件)
2017-02-13 18:37
976 查看
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前后台代码如下:
前台:
后台代码:
我们主页面的前后台代码如下:
前台:
后台代码:
//忽略
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(); } }
相关文章推荐
- 在数据库中 存储图片 以及 在界面中显示图片(存储图片路径)- 这种方法相对与存储二进制文件好
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- strusts2文件上传下载,以及二进制图片显示问题
- ASP.NET中文件上传下载方法集合(上传限制,保存方式-二进制或文件形式,上传Internet上的资源 )
- 第三方Girdview中文件下载的方法,以及js显示图片
- Win7系统中下载保存文件时不显示桌面选项的解决方法图解
- 在OpenLayers4中将地图及叠加显示的图层生成图片保存到数据库方法
- Win form 在pictureBox控件中实现选择图片然后以二进制方式保存到数据库。以及在pictureBox控件显示出来
- ASP.NET MVC文件上传下载Demo(文件保存在数据库)
- 把文件以二进制格式或base64字符串形式保存到数据库中,并实现下载功能源码
- Asp.net 中图片存储数据库以及页面读取显示通用方法详解-附源码下载
- ASP.NET中文件上传下载方法集合(上传限制,保存方式-二进制或文件形式,上传Internet上的资源 )
- R图片保存到PDF文件中顺利显示中文的方法
- 从数据库获取流文件,网页显示图片的方法~~
- ASP.NET中文件上传下载方法集合(上传限制,保存方式-二进制或文件形式,上传Internet上的资源 )
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- C# 用二进制将图片保存在数据库里再读出来显示在pictureBox中
- C#保存图片到数据库并读取显示图片的方法
- asp.net FileUpload 控件上传文件 以二进制的形式存入数据库并将图片显示出来