上传图片到SQL Server 2005使用ASP.Net MVC? [英] Upload images to SQL Server 2005 using ASP.Net MVC?

查看:293
本文介绍了上传图片到SQL Server 2005使用ASP.Net MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一种方法可以将图像上传到数据库图像类型或varbinary类型,但是,我搜索周围的整个星期,我无法找到任何可以帮助我,所以这真的是我的最后一招,如果有人知道如何上传图片到数据库中,我使用的SQL Server 2005前preSS。

I know there is a way to upload images to the database as image type or varbinary type, however, I searched around the entire week, I am unable to find anything that can help me, so this is really my last resort, if anybody know how to upload images to the database, I am using SQL Server 2005 Express.

感谢

推荐答案

您应该能够访问请求的文件的收集和获取HttpPostedFile实例,每个上传的文件。从文件抓斗的InputStream和读入字节数组列属性。我假设这是怎么了你的DAL映射VARBINARY您的业务类 - 如果不是,说这是一个原生图像,那么你就需要在保存之前进行转换。下面的示例使用LINQ2SQL。

You should be able to access the Request's File collection and obtain an HttpPostedFile instance for each uploaded file. Grab the InputStream from the file and read it into the byte array for the column property. I'm assuming this is how your DAL maps the varbinary to your business class -- if not, say it's a native Image, then you'll need to do the conversion before saving. The example below uses LINQ2SQL.

MyClass obj = new MyClass();
obj.Name = Request["name"];   // other properties
obj.Alt = Request["altText"];

HttpPostedFile file = Request.Files[0];
if (file != null)
{
     obj.Image image = new byte[file.ContentLength];
     file.Read(obj.Image,0,file.ContentLength];
}

using (DataContext context = new DataContext())
{
    context.InsertOnSubmit( obj );
    context.SubmitChanges();
}

这篇关于上传图片到SQL Server 2005使用ASP.Net MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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