Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvc3中处理图像的不同方法_C#_Asp.net Mvc 3_Database Design_File Upload - Fatal编程技术网

C# mvc3中处理图像的不同方法

C# mvc3中处理图像的不同方法,c#,asp.net-mvc-3,database-design,file-upload,C#,Asp.net Mvc 3,Database Design,File Upload,我有一个测试房地产应用程序。性能差的问题。一个属性可以有一个或多个图像 这些图像作为二进制文件存储在数据库中,现在我想创建一个更好的解决方案,在服务器硬盘中保存图像,在数据库中使用引用路径 我不知道怎么做。我应该如何设计我的db表?如何将映像保存到服务器及其到数据库的路径?我将发布我当前的代码 值得一提的是,我需要一个解决方案,我应该上传图像和其他属性信息(在同一页上,使用浏览按钮) 实体 public class Property { public Guid Id {get; set;}

我有一个测试房地产应用程序。性能差的问题。一个属性可以有一个或多个图像

这些图像作为二进制文件存储在数据库中,现在我想创建一个更好的解决方案,在服务器硬盘中保存图像,在数据库中使用引用路径

我不知道怎么做。我应该如何设计我的db表?如何将映像保存到服务器及其到数据库的路径?我将发布我当前的代码

值得一提的是,我需要一个解决方案,我应该上传图像和其他属性信息(在同一页上,使用浏览按钮)

实体

public class Property
{
   public Guid Id {get; set;}
   public string Name {get; set;}
   ...
   public List<Photo>Photos {get; set;}
}

public class Photo
{
   public Property Property {get; set;}
   public byte[] ImageData {get; set;}
   public string ImageMimeType {get; set;}
}
处理控制器内发布的图像和其他数据

 [HttpPost]
    public ActionResult Create(PropertyViewModel newData, IEnumerable<HttpPostedFileBase> images)
    {
        if (ModelState.IsValid)
        {
            using (ISession session = ...GetCurrentSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    Domain.Property model = new Domain.Property();
                    newData.ToDomainModel(model, images);
                    transaction.Commit();
                    session.Save(model);
                }
            }
            return RedirectToAction("Index");
        }
        else
        {
            return View(newData);
        }
    }
[HttpPost]
公共操作结果创建(PropertyViewModelNewData、IEnumerable图像)
{
if(ModelState.IsValid)
{
使用(ISession会话=…GetCurrentSession())
{
使用(ITransaction transaction=session.BeginTransaction())
{
Domain.Property model=新的Domain.Property();
newData.ToDomainModel(模型、图像);
Commit();
session.Save(模型);
}
}
返回操作(“索引”);
}
其他的
{
返回视图(newData);
}
}
最后是inside ViewModel.ToDomain,我会像这样保存发布的图像

请注意,IEnumerable图像是从webform发布的

List<Photo> Photos = new List<Photo>();
            foreach (var image in Images)
            {
                if (image != null && image.ContentLength > 0)
                {
                    Photo p = new Photo();
                    p.Property = x;
                    p.ImageMimeType = image.ContentType;
                    p.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(p.ImageData, 0, image.ContentLength);

                    Photos.Add(p);
                }
            }
            x.Photos = new List<Photo>();
            x.Photos = Photos;
List Photos=newlist();
foreach(图像中的var图像)
{
if(image!=null&&image.ContentLength>0)
{
照片p=新照片();
p、 属性=x;
p、 ImageMimeType=image.ContentType;
p、 ImageData=新字节[image.ContentLength];
image.InputStream.Read(p.ImageData,0,image.ContentLength);
照片。添加(p);
}
}
x、 照片=新列表();
x、 照片=照片;

这种方法有效,实体之间的映射是可以的,现在我应该更改什么才能将图像保存在服务器hdd上及其对db的引用。

将数据作为原始文件存储在db服务器磁盘上有其自身的问题,主要问题是您必须配置从客户端访问文件系统的权限—在本例中是web服务器

如果您使用的是Sql Server 2008或更高版本,则有一个折衷方案:


数据存储在文件中,但可以通过数据库访问。因此,您可以通过数据库连接获得引用完整性和访问权限,而不会使数据库本身膨胀。

实际上我使用的是sqlserver2008R2,因此我将检查此信息。在将图像上载到服务器之前,您是否进行过客户端大小调整?请注意,这很可能是性能问题的原因,在文件系统上保存映像与在数据库中保存映像相比不太可能提高性能。不,情况并非如此。仔细检查。
List<Photo> Photos = new List<Photo>();
            foreach (var image in Images)
            {
                if (image != null && image.ContentLength > 0)
                {
                    Photo p = new Photo();
                    p.Property = x;
                    p.ImageMimeType = image.ContentType;
                    p.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(p.ImageData, 0, image.ContentLength);

                    Photos.Add(p);
                }
            }
            x.Photos = new List<Photo>();
            x.Photos = Photos;